sql:谓词

谓词就是返回值为真值的函数。

1.like谓词:字符串的部分一致查询

取ddd开头的字符串:

 
  1. select *

  2. from SampleLike

  3. where strcol like 'ddd%';

%代表0字符以上的任意字符串,是通配符其中的一种

2.between谓词:范围查找

选取销售单价为100-1000的商品:

 
  1. select product_name, sale_price

  2. from Product

  3. where sale_price between 100 and 1000;

头尾都会取到:

3.is null和is not null判断是否为null

选取某些值为null的列不能用=,只能用is null特定的谓词。

 
  1. select product_name, purchase_price

  2. from Product

  3. where purchase_price is null;

is not null就是将值不为null的取出。

4.in谓词,or的简变方法

需要指定多个值来进行条件判断查询时,使用in来代替or

 
  1. select product_name, purchase_price

  2. from Product

  3. where purchase_price in (320, 500, 5000);

只要在括号里的值全部取出:

反之,not in就是不取括号里的所有值。

5.使用子查询作为in谓词的参数

in谓词具有其他谓词里所没有的用法,就是可以使用子查询作为其参数。

选取商店编号为'000C'的所有销售商品的单价:

 
  1. select product_name, sale_price

  2. from Product

  3. where product_id in (select product_id

  4. from ShopProduct

  5. where shop_id = '000C');

先在子查询中选取到这个商店的所有的商品编号,然后在主查询中查询出这些商品编号所对应的单价。

类似如下查询:括号里就是子查询的结果。

 
  1. select product_name, sale_price

  2. from Product

  3. where product_id in ('0003', '0004', '0006', '0007');

6.exists谓词:exists的意思为存在,就是只要exists的参数存在,exists谓词结果就为真,否则为假。一般情况下exists的参数选择为关联子查询:

使用exists查询之前in查询的结果:

 
  1. select product_name, sale_price

  2. from Product as p

  3. where exists (select *

  4. from ShopProduct as sp

  5. where sp.shop_id = '000C'

  6. and sp.product_id = p.product_id);

结果:

一样的结果。由于exists只关心记录是否存在,因此返回哪些列都没有关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值