mysql 2、条件查询
进阶2:条件查询
语法:
select 查询列表 from 表名 where 筛选条件;
分类:
一:按条件表达式筛选
条件运算符: > < = !=(<>) >= <=
二:按逻辑表达式筛选
作用:用于连接条件表达式
and:并
or:或
not:非
逻辑运算符:and or not
三:模糊查询
like between and in is null
#一、按条件表达式筛选
案例1:查询工资>12000的员工信息
select * from user where gz>12000;
案例2:查询不等于90号的员工名和部门编号
select * from user where code<>90;
#二、按逻辑表达式筛选
#案例一:查询工资在10000到20000之间的员工名;
select name from user where salary>=10000 and salary<=20000;
#案例二:查询部门编号不是在90到110之间,或者工资高于15000的员工信息
select * from name where NOT(code>=90 and code<=110) or salary>15000;
#三、模糊查询
like
between and
in
is null | is not null
#1、like
#案例1:查询员工名中包含字符a的员工信息
select * from user where name like '%a%'
#案例2:查询员工名中第三个字符未e,第五个字符为a的员工名;
select name from user where name like '__e_a%'
#案例3:查询员工名中第二个字符为_的员工名
select name from user where name like '_\_%'
select name from user where name like '_$_%' escape '$';
#2、between and
1.使用between and 可以提高语句的简洁度
2.between and 有2个临界值
3、不能调转顺序
#案例一、查询员工编号在100到120之间的员工信息
select * from user where code >=100 and code<=120
------------------------------------------------------------------
select * from user where between 100 and 120
#3、in
含义:判断某字段的值是否属于in列表中的某一项
1.使用in提高语句简洁度
2.要求值同样类型
#案例一:查询员工的工种编号是 1,2,3中的员工
select * from user where code='1' or code='2' or code='3';
------------------------------------------------------------------
select * from user where code in('1','2','3');
#4、in null
#案例 一:查询没有code的员工
select * from user where code is null;
#案例二:查询有code的员工
select * from user where code in not null;
#安全等于 <=>
#案例 一:查询没有code的员工
select * from user where code <=>null;
#案例 二:查询code=2的员工
select * from user where code <=>2;

浙公网安备 33010602011771号