小编今天给大家带来的是,SQL基本语句中的条件语句查询WHERE,希望对大家学习和使用SQL有帮助。
where后面支持多种运算符,进行条件的处理
WHERE 用于条件过滤记录,也就是将访问数据进行条件处理,缩小到一定范围内。
1.比较运算符
比较运算符 |
描述 |
= |
等于 |
<>或 != |
不等于。 |
> |
大于 |
< |
小于 |
>= |
大于等于 |
<= |
小于等于 |
例:查询名字是张三的顾客
select * from customers where name = '张三';
2.逻辑运算符
逻辑运算符 |
描述 |
and |
两边条件都要满足 |
or |
两边条件任意满足一个 |
not |
否定一个 |
注意:and优先级高于or
例:查询名字是张三,且没有被删除的顾客
select * from customers where name = '张三' and is_delete=0
3.模糊查询
Like
Like 支持两个通配符匹配选项:% 和 _。
% 表示任一字符出现任意多次。
_ 表示任一字符出现一次。
注意:使用通配符需要注意,将通配符置于开头处,匹配会变得非常慢。
查询姓张或名字叫三的顾客
select * from customers where name like '张%' or name like '%三';
4.范围查询
范围查询 |
描述 |
in |
特定值中任选一个值 |
between |
between … and … 一个特定、连续范围内 |
查询价格是3到5的商品
select * from products where pric between 3 and 5
查询价格是3或者是5的商品
select * from products where pric in (3.5)
5.空判断
null
is null 判断为空
is not null 判断为非空
查询标有价格的商品
select * from products where pric is null
查询没有标价格的商品
select * from products where pric is not null
数据分析咨询请扫描二维码