MySQL常见的查询——2


条件查询:
select 查询列表 from 表名 where 筛选条件;
分类:
一:按条件表达式进行筛选
简单条件运算符:< > = != <> >= <=
二:按照逻辑表达式进行筛选
逻辑运算符:&& || ! and or NOT
&& and 表示的是:两个都为true的时候,结果才为true
|| or 表示的是:其中一个结果为true的时候,结果就为true
! not 表示的是:取反
三:模糊查询
like
BETWEEN
in
is null
is not null


#查询部门编号不等于90的员工号和部门号
select last_name,department_id from employees where department_id!='90'
select last_name,department_id from employees where department_id<>'90'--推荐使用这种方式

#查询部门编号不在90-110之间,或者薪水在10000以上的(下面这两种写法都是正确的)
select * from employees a where a.department_id<90 or a.department_id>110 or a.salary>100000
select * from employees a where not(a.department_id>=90 and a.department_id<=110 ) or a.salary>100000

#其中_表示的是单个字符的模糊匹配
#查询员工姓名中第三个字符是a ,第五个字符为e的员工的姓名和工资
select last_name,salary from employees where last_name like '__a_e';

#查询姓名中第二个字符为_的员工姓名
#其中\表示的是转义
select last_name from employees where last_name like '_\_%'
#或者 自定义转移符号
select last_name from employees where last_name like '_$_%' ESCAPE '$'

#使用between and 查询员工编号在100-120之间的员工信息
select * from employees where employee_id BETWEEN 100 and 120;

猜你喜欢

转载自www.cnblogs.com/dongyaotou/p/12297275.html
今日推荐