一、条件查询
1、按条件表达式筛选
> < = != <> >= <=
2、按逻辑表达式筛选
用于连接条件表达式
逻辑运算符:and or not
3、模糊查询(可以与条件表达式筛选归为一类)
like
一般和通配符搭配使用:
% 表示任意字符,包含0个字符
例如要查包含姓包含a的员工
select * from employees where last_name like '%a%';
_ 表示单个字符
例如要查询第三个字符为a的员工
select * from employees where last_name like '__a%';
between...and... 包含临界值
in
is null / is not null
# 查询部门编号不是在90和100之前,或工作高于15000的员工信息
select * from employees where not(department_id BETWEEN '80' and '110') or salary>15000;

 

 
二、转义字符
在mysql中可以使用like进行模糊查询,like语法中_表示单个关键字,这时如果要求使用like查询第二个字为_的字段数据,就需要用到转义字符。
方法一:加\
select * from employees where last_name like '_\_%';
方法二:使用关键字ESCAPE指定转义字符(推荐使用这种方法)
使用ESCAPE指定$(可以是任意字符)为转义字符
select * from employees where last_name like '_$_%' ESCAPE '$';
 
三、安全等于:<=>
is null:只能判断null值,可读性较高
<=>:既可以判断null又可以判断普通的数值,可读性较低
# 查询员工工资为12000的员工
select last_name,salary from employees where salary<=>12000;
# 查询奖金率为null的员工
select last_name,commission_pct from employees where commission_pct<=>NULL;

 

四、ISNULL
判断字段值或者表达式是否为null
如果为null则返回1,不为null则返回0
select ISNULL(commission_pct),commission_pct from employees;

 

 
posted on 2022-07-17 21:46  时光以北暮南城  阅读(37)  评论(0编辑  收藏  举报