五、条件查询
5、条件查询
条件查询需要用到where语句,where必须放到from语句表的后面
支持如下运算符
运算符 |
说明 |
= |
等于 |
<>或!= |
不等于 |
< |
小于 |
<= |
小于等于 |
> |
大于 |
>= |
大于等于 |
between … and …. |
两个值之间,等同于 >= and <= |
is null |
为null(is not null 不为空) |
and |
并且 |
or |
或者 |
in |
包含,相当于多个or(not in不在这个范围中) |
not |
not可以取非,主要用在is 或in中 |
like |
like称为模糊查询,支持%或下划线匹配 %匹配任意个字符 下划线,一个下划线只匹配一个字符 |
5.1、等号操作符
- 查询薪水为5000的员工
select empno, ename, sal from emp where sal=5000; |
2.查询job为MANAGER的员工
job为字符串,所以需要加上引号, |
select empno, ename from emp where job="manager"; 以上输出正确,Mysql默认情况下大小写是不敏感的。 |
注意:
MySQL在windows下是不区分大小写的,将script文件导入MySQL后表名也会自动转化为小写,结果再 想要将数据库导出放到linux服务器中使用时就出错了。因为在linux下表名区分大小写而找不到表,查了很多都是说在linux下更改MySQL的设置使其也不区分大小写,但是有没有办法反过来让windows 下大小写敏感呢。其实方法是一样的,相应的更改windows中MySQL的设置就行了。
具体操作:
在MySQL的配置文件my.ini中增加一行:
lower_case_table_names = 0
其中 0:区分大小写,1:不区分大小写
MySQL在Linux下数据库名、表名、列名、别名大小写规则是这样的:
1、数据库名与表名是严格区分大小写的;
2、表的别名是严格区分大小写的;
3、列名与列的别名在所有的情况下均是忽略大小写的;
4、变量名也是严格区分大小写的; MySQL在Windows下都不区分大小写
5.2、 <>操作符
- 查询薪水不等于5000的员工
select empno, ename, sal from emp where sal <> 5000; |
以下写法等同于以上写法,建议使用第一种写法
select empno, ename, sal from emp where sal != 5000; |
2.查询工作岗位不等于MANAGER的员工
select empno, ename,job from emp where job <> 'MANAGER'; |
5.3、between … and …操作符
- 查询薪水为1600到3000的员工(第一种方式,采用>=和<=)
select empno, ename, sal from emp where sal >= 1600 and sal <= 3000; |
2.查询薪水为1600到3000的员工(第一种方式,采用between … and …)
select empno, ename, sal from emp where sal between 1600 and 3000; |
关于between … and …,它是包含最大值和最小值的
5.4、is null
- Null为空,但不是空串,为null可以设置这个字段不填值,如果查询为null的字段,采用is null
- 查询津贴为空的员工
select * from emp where comm=null; |
以上也无法查询出符合条件的数据,因为null类型比较特殊,必须使用 is来比较
select * from emp where comm is null; |
5.5、 and
and表示并且的含义,表示所有的条件必须满足
- 工作岗位为MANAGER,薪水大于2500的员工
select * from emp where job='MANAGER' and sal > 2500; |
5.6、or
or,只要满足条件即可,相当于包含
- 查询出job为manager或者job为salesman的员工
select * from emp where job='MANAGER' or job='SALESMAN'; |
5.7、表达式的优先级
- 查询薪水大于1800,并且部门代码为20或30的员工(错误的写法)
select * from emp where sal > 1800 and deptno = 20 or deptno = 30; |
以上输出不是预期结果,薪水小于1800的数据也被查询上来了,原因是表达式的优先级导致的,首先过滤sal > 1800 and deptno = 20,然后再将deptno = 30员工合并过来,所以是不对的
- 查询薪水大于1800,并且部门代码为20或30的(正确的写法)
select * from emp where sal > 1800 and (deptno = 20 or deptno = 30); |
关于运算符的问题:不用记,没有把握尽量采用括号
5.8、in
in表示包含的意思,完全可以采用or来表示,采用in会更简洁一些
- 查询出job为manager或者job为salesman的员工
select * from emp where job in ('manager','salesman'); |
- 查询出薪水包含1600和薪水包含3000的员工
select * from emp where sal in(1600, 3000); |
5.9、not
- 查询出薪水不包含1600和薪水不包含3000的员工(第一种写法)
select * from emp where sal <> 1600 and sal <> 3000; |
2.查询出薪水不包含1600和薪水不包含3000的员工(第二种写法)
select * from emp where not (sal = 1600 or sal = 3000); |
3.查询出薪水不包含1600和薪水不包含3000的员工(第三种写法)
select * from emp where sal not in (1600, 3000); |
4.查询出津贴不为null的所有员工
select * from emp where comm is not null; |
5.10、like
- Like可以实现模糊查询,like支持%和下划线匹配
- 查询姓名以M开头所有的员工
select * from emp where ename like 'M%'; |
3.查询姓名以N结尾的所有的员工
select * from emp where ename like '%N'; |
4.查询姓名中包含O的所有的员工
select * from emp where ename like '%O%'; |
5.查询姓名中第二个字符为A的所有员工
select * from emp where ename like '_A%'; |
Like中%和下划线的差别?
%匹配任意字符出现的个数
下划线只匹配一个字符
Like 中的表达式必须放到单引号中|双引号中,以下写法是存在错误的:
select * from emp where ename like _A% |