语法
- select 查询列表 from 表名 where 筛选条件;
比较运算符 | >、< 、>=、<=、= 、<> | 大于、小于、大于等于、小于等于、等于、不等于 |
---|---|---|
BETWEEN ...AND... | 显示在某一区间的值(含头含尾) | |
IN(set) | 显示在in列表中的值,例:in(100,200) | |
LIKE 通配符 | 模糊查询,Like语句中有两个通配符:% 用来匹配多个字符;例first_name like ‘a%’;_ 用来匹配一个字符。例first_name like ‘a_’; | |
逻辑运算符 | and (&&) | 多个条件同时成立 |
or (||) | 多个条件任一成立 | |
not (!) | 不成立,例:where not(salary>100); |
按条件表达式筛选
-
查询工资>12000的员工信息
select * from stuinfo where name = 'xiaoli';
select name from stuinfo where id=3;
select id, name from stuinfo where id=3; -
按逻辑表达式筛选
select id, name from stuinfo where id=3 and name='xiaoli';
select id, name from stuinfo where id=3 and name='xiaohong';
select * from stuinfo where id=1 or name=null;
select * from stuinfo where id=1 or name='xiaohong';
select * from stuinfo where id=1 or name='xiaoli';
select id, name from stuinfo where not (id=1) or name is not null; -
模糊查询
select * from stuinfo where name like '%xiao%';
select * from stuinfo where name like 'x__o%';
select * from stuinfo where name like '__';
-
IN 判断某字段的值是否属于in列表中的某一项 特点 列表的值类型必须一致或兼容 列表中不支持通配符
select id, name from stuinfo where id in(1, 3); -
IS NULL =或<>不能用于判断null值 is null或is not null 可以判断null值
select id, name from stuinfo where id is null;
select id, name from stuinfo where name is null;
select id, name from stuinfo where id is not null;
select id, name from stuinfo where name is not null; -
安全等于 <=> IS NULL:仅仅可以判断NULL值,可读性较高,建议使用 <=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
select id, name from stuinfo where id <=> 1;
网友评论