where子句

where子句:将过滤条件放在where子句的后面,可以筛选/过滤出我们想要的符合条件的数据

1.where子句 + 关系运算符 > < = >= <=  <>不等于 !=不等于

查询deptno=10的:select * from emp where deptno = 10;

运行结果:

 

查询job='clerk'的:select * from emp where job = 'clerk';  (不区分单双引号,大小写,但是加了binary就区分大小写了)

运行结果:

 

 

查询job='CLERK'的:select * from emp where binary job = "CLERK";(区分大小写)

运行结果:

 

 

 2.where子句 + 逻辑运算符  add(&&)与  or(||)或

sal不包含1500,和3000:select * from emp where sal>1500 and sal<3000; 等价于:select * from emp where sal>1500 && sal<3000; 

运行结果:

 

 

 sal排序:select * from emp where sal>1500 and sal<3000 order by sal;

运行结果:

 

 

 sal包含1500和3000:select * from emp where sal between 1500 and 3000; 

运行结果:

 

 

查询deptno等于10或者20的select * from emp where deptno = 10 or deptno = 20;

                                                 select * from emp where deptno = 10 || deptno = 20;

                                                 select * from emp where deptno in(10,20);   -- in可接数字,也可接字符 (三条数据相同)

 

运行结果:

 

 

 3.模糊查询

查询名字带a的员工:select * from emp where ename like'%a%';   (%代表任意多个字符0,1,2,3)

运行结果:

 

 

 查询第二个字符是a的:select * from emp where ename like'_a%';   (第二个字符是a的   _代表任意一个字符)

 运行结果:

 

 

 4.关于null的判断

判断comm是空的:select * from emp where comm is null;

运行结果:

 

 

 判断comm不是空的select * from emp where comm is not null;

运行结果:

 

 

 5.小括号 优先级别不同,加括号为了可读性

看先or还是and:select * from emp where job = 'salesman' or job = 'clerk' and sal >= 1500;    (先and在or)

运行结果:

 

 

select * from emp where job = 'salesman' or (job = 'clerk' and sal >= 1500);

 

运行结果:

 

 

 与上面等同,所以说:先and再or and>or

 

select * from emp where (job = 'salesman' or job = 'clerk') and sal >= 1500;

 运行结果:

 

posted @ 2021-12-23 19:23  努力学爪哇  阅读(226)  评论(0编辑  收藏  举报