条件查询需要用到where语句,where必须放到from语句表的后面 支持如下运算符:
等号操作符:
1)查询薪水为5000的员工
select ename, sal from emp where sal=5000;
2)查询job为MANAGER的员工
select ename from emp where job='manager'; // 记得加引号 如果他是字符串
<>操作符 不等于操作符
1)查询工资不等于5000的员工
select ename, sal from emp ename where sal <> 5000;
select ename, sal from emp ename where sal != 5000; 不建议这么写
between … and …操作符
查询薪水为1600到3000的员工(第一种方式,采用>=和<=)
1)select ename, sal from emp where sal >= 1600 and sal <= 3000;
2)select ename, sal from emp where sal between 1600 and 3000;
注:between … and …,它是包含最大值和最小值的
is null
Null为空,但不是空串,为null可以设置这个字段不填值,如果查询为null的字段,采用is null
1) 查询 绩效comm 为空的员工
select * from emp where comm=null; 查不出来!!
因为null类型比较特殊,必须使用 is来比较
2)select * from emp where comm is null;
表达式的优先级
查询薪水大于1800,并且部门代码为20或30的员工(错误写法)
select * from emp where sal > 1800 and deptno = 20 or deptno = 30;
查询薪水大于1800,并且部门代码为20或30的员工 正确的
select * from emp where sal > 1800 and (deptno = 20 or deptno = 30);
in
in表示包含的意思,完全可以采用or来表示,采用in会更简洁一些
查询出job为manager或者job为salesman的员工
select * from emp where job in ('manager','salesman');
not
1)查询出薪水不包含1600和薪水不包含3000的员工
方法一:select * from emp where sal <> 1600 and sal <> 3000;
方法二:select * from emp where not (sal = 1600 or sal = 3000);
方法三:select * from emp where sal not in (1600, 3000);
2)查询出津贴不为null的所有员工
select * from emp where comm is not null;
like
Like可以实现模糊查询,like支持%和下划线匹配 查询姓名以M开头所有的员工
select * from emp where ename like 'M%'; // 注意:引号 不带引号就错了
Like中%和下划线的差别?
%匹配任意字符出现的个数
下划线只匹配一个字符