1、作用
where使用来过滤数据的,比如有以下查询语句
select name,age from user_message where age>=25
过滤出user_message 表里,age列大于等于25的列(name,price两列)
2、where子句的操作符
操作符 | 说明 | 举例 |
---|---|---|
= | 等于 | Select name, price from products where prod_name = ‘fuses’ |
<> | 不等于 | Select id, name from products where id <> 1003 |
!= | 不等于 | |
> | 大于 | / |
>= | 大于等于 | / |
< | 小于 | Select name, price from products where prod_price < ‘10’ |
<= | 小于等于 | Select name, price from products where prod_price < = ‘10’ |
between A and B | 在指定的两个值之间 | Select name, price from products where price between 5 and 10 |
is | 指定数值/检查空值 | Select id,name,price from products where name is null |
like | 模糊查询 ,搜索匹配字段中的指定内容 | WHERE column LIKE pattern |
not like | 模糊查询,搜索匹配字段中的指定内容 | WHERE column NOT LIKE pattern |
and | where子句组合操作符-并且 | where a=1 and B=2 |
or | where子句组合操作符 -或者 | where a=1 or B=2 |
3、组合where子句
Mysql允许给出多个where子句,子句可以以两种方式使用;以and子句和or 子句的方式使用
(1)and操作符
取出表中id=1003和price< = 10;的数据
Select id, name, price, from products where id = 1003 and price < = 10
(2)or操作符
取出表中id id 等于1002,或id 等于1003的数据
Select id, name, price from products where id = 1002 or id = 1003
(3)计算次序
价格为10元以上,且有1002或1003的所有产品
select id,name,price from products where price>10 and(id=1002 or id=1003)