条件查询

  • select 查询列表 from 表名 where 筛选条件;
    语法格式: 执行顺序:先from,然后where,最后select
select 
   字段,字段……
from
   表名
where
   条件;
比较运算符>、< 、>=、<=、= 、<>大于、小于、大于等于、小于等于、等于、不等于
BETWEEN ...AND...显示在某一区间的值(含头含尾),数据方面是闭区间,语句中左闭右开。
IN(set)显示在in列表中的值,例:in(100,200),等同于or。
LIKE 通配符模糊查询,Like语句中有两个通配符:% 用来匹配多个字符;例first_name like ‘a%’;_ 用来匹配一个字符。例first_name like ‘a_’;
逻辑运算符and (&&)多个条件同时成立
or (||)多个条件任一成立
not (!)不成立,例:where not(salary>100);,区非主要用户is 或则 in 中

查询工资等于5000的员工姓名?

select ename from emp where sal=5000;

查询SMITH的工资?(SMITH名字是字符串,需要单引号)

select sal from emp where ename = 'SMITH';

找出工资大于3000的员工,大于等于,小于,不等于,不等于

select ename,sal from emp where sal > 3000;
select ename,sal from emp where sal >= 3000;
select ename,sal from emp where sal < 3000;
select ename,sal from emp where sal <> 3000;
select ename,sal from emp where sal != 3000;

找出公司1100到3000之间的员工

select ename,sal from emp where sal >= 1100 and sal <= 3000;
select ename,sal from emp where sal between 1100 and 3000;
select ename,sal from emp where sal between ‘A’ and ‘C’;

ename为姓名、emp 为对应的表、sal为工资
between……and……是闭区间,必须左小右大
between……and……除了使用数字方面之外,还可以使用在字符串方面,是左闭右开

select ename,sal,comm from emp where comm is null
select ename,sal,comm from emp where comm is not null

在数据库中NULL 不是一个值,代表什么也没有,为空必须使用 is null 或则是不为空 is not null

找出哪些人没有津贴?

select ename,sal,comm from emp where comm is not null or comm = 0;

 

and 和 or and优先级别高

找出工作岗位MANAGER 和 SALESMAN 的员工

select ename,job from emp where job = 'MANAGER' or job 'SALESMAN';

and 和 or 联合起来用,找出薪资大于一千,并且部门标号是 20或则是30部门的员工。

select ename,sal,deputno from emp where sal > 1000 and deptno = 20 or deptno = 30;  //错误的
select ename,sal,deputno from emp where sal > 1000 and (deptno = 20 or deptno = 30);  //正确的

注意:当前运算符的优先级不确定的时候加小括号,加小括号的优先执行

 

in,in 等同于or

语法:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)

找出工作岗位是 MANAGER 和 SALESMAN 的员工?

select ename,job from emp where job = 'MANAGER' or job = 'SALESMAN';
select ename,job from emp where job in('MANAGER' ,'SALESMAN');
select ename,job from emp where job in(1000 ,5000);

 

like 模糊查询

select * from stuinfo where name like '%xiao%';
select * from stuinfo where name like 'x__o%';
select * from stuinfo where name like '__a';

找出名字中有下划线?

select * from stuinfo where name like '%\_%';

找出名字中最后一个字母是T?

select * from stuinfo where name like '%T';