12 条件查询。
语法格式:
select
字段,字段...
from
表名
where
条件;
执行顺序:先from,然后where,最后select
查询工资等于5000的员工姓名?
select ename from emp where sal = 5000;
+-------+
| ename |
+-------+
| KING |
+-------+
查询smith的工资?
select empno,ename,sal from emp where ename = 'smith'; // 字符串需要用单引号括起来。
+-------+-------+--------+
| empno | ename | sal |
+-------+-------+--------+
| 7369 | SMITH | 800.00 |
+-------+-------+--------+
查询工资高于3000的员工?
select empno,ename,sal from emp where sal >= 3000;
+-------+-------+---------+
| empno | ename | sal |
+-------+-------+---------+
| 7788 | SCOTT | 3000.00 |
| 7839 | KING | 5000.00 |
| 7902 | FORD | 3000.00 |
+-------+-------+---------+
select empno,ename,sal from emp where sal <= 3000;
select empno,ename,sal from emp where sal < 3000;
select empno,ename,sal from emp where sal > 3000;
找出工资不等于3000的?
select empno,ename,sal from emp where sal <> 3000;
select empno,ename,sal from emp where sal != 3000;
找出工资在1100~3000的人
select empno,ename,sal from emp where sal >= 1100 and sal <= 3000;
select empno,ename,sal from emp where sal between 1100 and 3000; // between...and...是闭区间[1100 ~ 3000];
select empno,ename,sal from emp where sal between 3000 and 1100; // 查询不到任何数据
between and 在使用的时候必须左小右大。
between and 除了可以使用在数字方面之外,还可以使用在字符串方面。
select empno,ename,sal from emp where ename between 'a' and 'c'; // 左闭右开 就是说包含a不包含c
+-------+-------+---------+
| empno | ename | sal |
+-------+-------+---------+
| 7499 | ALLEN | 1600.00 |
| 7698 | BLAKE | 2850.00 |
| 7876 | ADAMS | 1100.00 |
+-------+-------+---------+
找出那些人没有津贴?
在数据库当中null不是一个值,代表什么也没有,为空。
空不是一个值,不能用等号衡量。
必须使用is null或者is not null
找出哪些人的津贴为null?
select empno,ename,sal,comm from emp where comm is null;
找出那些人没有津贴?
select empno,ename,sal,comm from emp where (comm is null or comm = 0);
+-------+--------+---------+------+
| empno | ename | sal | comm |
+-------+--------+---------+------+
| 7369 | SMITH | 800.00 | NULL |
| 7566 | JONES | 2975.00 | NULL |
| 7698 | BLAKE | 2850.00 | NULL |
| 7782 | CLARK | 2450.00 | NULL |
| 7788 | SCOTT | 3000.00 | NULL |
| 7839 | KING | 5000.00 | NULL |
| 7844 | TURNER | 1500.00 | 0.00 |
| 7876 | ADAMS | 1100.00 | NULL |
| 7900 | JAMES | 950.00 | NULL |
| 7902 | FORD | 3000.00 | NULL |
| 7934 | MILLER | 1300.00 | NULL |
+-------+--------+---------+------+
找出那些人津贴不为null?
select empno,ename,sal,comm from emp where comm is not null;
+-------+--------+---------+---------+
| empno | ename | sal | comm |
+-------+--------+---------+---------+
| 7499 | ALLEN | 1600.00 | 300.00 |
| 7521 | WARD | 1250.00 | 500.00 |
| 7654 | MARTIN | 1250.00 | 1400.00 |
| 7844 | TURNER | 1500.00 | 0.00 |
+-------+--------+---------+---------+
找出工作岗位是manager和salesman的员工?
select empno,ename,sal,comm,job from emp where job = 'salesman' or job = 'manager';
+-------+--------+---------+---------+----------+
| empno | ename | sal | comm | job |
+-------+--------+---------+---------+----------+
| 7499 | ALLEN | 1600.00 | 300.00 | SALESMAN |
| 7521 | WARD | 1250.00 | 500.00 | SALESMAN |
| 7566 | JONES | 2975.00 | NULL | MANAGER |
| 7654 | MARTIN | 1250.00 | 1400.00 | SALESMAN |
| 7698 | BLAKE | 2850.00 | NULL | MANAGER |
| 7782 | CLARK | 2450.00 | NULL | MANAGER |
| 7844 | TURNER | 1500.00 | 0.00 | SALESMAN |
+-------+--------+---------+---------+----------+
select empno,ename,sal,comm,job from emp where job in ('salesman','manager');
+-------+--------+---------+---------+----------+
| empno | ename | sal | comm | job |
+-------+--------+---------+---------+----------+
| 7499 | ALLEN | 1600.00 | 300.00 | SALESMAN |
| 7521 | WARD | 1250.00 | 500.00 | SALESMAN |
| 7566 | JONES | 2975.00 | NULL | MANAGER |
| 7654 | MARTIN | 1250.00 | 1400.00 | SALESMAN |
| 7698 | BLAKE | 2850.00 | NULL | MANAGER |
| 7782 | CLARK | 2450.00 | NULL | MANAGER |
| 7844 | TURNER | 1500.00 | 0.00 | SALESMAN |
+-------+--------+---------+---------+----------+
and和or联合起来用:找出薪资大于1000的并且部门编号是20或30部门的员工。
select empno,ename,sal,comm,job,deptno from emp where sal > 1000 and deptno = 20 or deptno = 30; // 错误的
select empno,ename,sal,comm,job,deptno from emp where sal > 1000 and (deptno = 20 or deptno = 30);
+-------+--------+---------+---------+----------+--------+
| empno | ename | sal | comm | job | deptno |
+-------+--------+---------+---------+----------+--------+
| 7499 | ALLEN | 1600.00 | 300.00 | SALESMAN | 30 |
| 7521 | WARD | 1250.00 | 500.00 | SALESMAN | 30 |
| 7566 | JONES | 2975.00 | NULL | MANAGER | 20 |
| 7654 | MARTIN | 1250.00 | 1400.00 | SALESMAN | 30 |
| 7698 | BLAKE | 2850.00 | NULL | MANAGER | 30 |
| 7788 | SCOTT | 3000.00 | NULL | ANALYST | 20 |
| 7844 | TURNER | 1500.00 | 0.00 | SALESMAN | 30 |
| 7876 | ADAMS | 1100.00 | NULL | CLERK | 20 |
| 7902 | FORD | 3000.00 | NULL | ANALYST | 20 |
+-------+--------+---------+---------+----------+--------+
注意:当运算符的优先级不确定的时候加小括号。
in等同于or:找出工作岗位是manager和salesman的员工?
select empno,ename,sal,job,comm,deptno from emp where job in ('manager','salesman');
+-------+--------+---------+----------+---------+--------+
| empno | ename | sal | job | comm | deptno |
+-------+--------+---------+----------+---------+--------+
| 7499 | ALLEN | 1600.00 | SALESMAN | 300.00 | 30 |
| 7521 | WARD | 1250.00 | SALESMAN | 500.00 | 30 |
| 7566 | JONES | 2975.00 | MANAGER | NULL | 20 |
| 7654 | MARTIN | 1250.00 | SALESMAN | 1400.00 | 30 |
| 7698 | BLAKE | 2850.00 | MANAGER | NULL | 30 |
| 7782 | CLARK | 2450.00 | MANAGER | NULL | 10 |
| 7844 | TURNER | 1500.00 | SALESMAN | 0.00 | 30 |
+-------+--------+---------+----------+---------+--------+
in后面的值不是区间,是具体的值。
not in: 不在这几个值当中。
select empno,ename,sal,job,comm,deptno from emp where job not in ('manager','salesman');
+-------+--------+---------+-----------+------+--------+
| empno | ename | sal | job | comm | deptno |
+-------+--------+---------+-----------+------+--------+
| 7369 | SMITH | 800.00 | CLERK | NULL | 20 |
| 7788 | SCOTT | 3000.00 | ANALYST | NULL | 20 |
| 7839 | KING | 5000.00 | PRESIDENT | NULL | 10 |
| 7876 | ADAMS | 1100.00 | CLERK | NULL | 20 |
| 7900 | JAMES | 950.00 | CLERK | NULL | 30 |
| 7902 | FORD | 3000.00 | ANALYST | NULL | 20 |
| 7934 | MILLER | 1300.00 | CLERK | NULL | 10 |
+-------+--------+---------+-----------+------+--------+
模糊查询like?
找出名字当中含有O的?(在模糊查询当中,必须掌握两个特殊的符号,一个是%,一个是_)
%代表任意多个字符,_代表任意1个字符。
select ename from emp where ename like '%O%';
+-------+
| ename |
+-------+
| JONES |
| SCOTT |
| FORD |
+-------+
找出第二个字母是A的?
select ename from emp where ename like '_A%';
+--------+
| ename |
+--------+
| WARD |
| MARTIN |
| JAMES |
+--------+
找出名字中有下划线的?
select ename from emp where ename like '%\_%';
找出名字中最后一个字母是T的?
select ename from emp where ename like '%T';
+-------+
| ename |
+-------+
| SCOTT | 外文:orcale数据库当中有一个用户scott,密码是tiger。
+-------+