Operating table
mysql> select * from emp;
EMPNO | ENAME | JOB | MGR | HIREDATE | SHALL | COMM | DEPTNO |
---|---|---|---|---|---|---|---|
7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
The field names of each field are employee number, employee name, employee position, employee's superior leader number, employee employment date, employee salary, employee allowance, employee department number.
Sort (ascending, descending)
In ascending order of salary, find out the employee's name and salary?
select
ename,sal
from
emp
order by
sal;
ename
|
shall
|
---|---|
SMITH | 800.00 |
JAMES | 950.00 |
ADAMS | 1100.00 |
WARD | 1250.00 |
MARTIN | 1250.00 |
MILLER | 1300.00 |
TURNER | 1500.00 |
ALLEN | 1600.00 |
CLARK | 2450.00 |
BLAKE | 2850.00 |
JONES | 2975.00 |
SCOTT | 3000.00 |
FORD | 3000.00 |
KING | 5000.00 |
Note: The default is ascending order. How to specify ascending or descending order? asc means ascending order, desc means descending order.
select ename , sal from emp order by sal; // 升序
select ename , sal from emp order by sal asc; // 升序
select ename , sal from emp order by sal desc; // 降序。
Arrange in descending order of salary, and arrange in ascending order of name when the salary is the same.
select ename,sal from emp ORDER BY sal desc;
ename | shall |
---|---|
KING | 5000.00 |
SCOTT | 3000.00 |
FORD | 3000.00 |
JONES | 2975.00 |
BLAKE | 2850.00 |
CLARK | 2450.00 |
ALLEN | 1600.00 |
TURNER | 1500.00 |
MILLER | 1300.00 |
WARD | 1250.00 |
MARTIN | 1250.00 |
ADAMS | 1100.00 |
JAMES | 950.00 |
SMITH | 800.00 |
select ename,sal from emp ORDER BY sal desc,ename asc;
ename | shall |
---|---|
KING | 5000.00 |
FORD | 3000.00 |
SCOTT | 3000.00 |
JONES | 2975.00 |
BLAKE | 2850.00 |
CLARK | 2450.00 |
ALLEN | 1600.00 |
TURNER | 1500.00 |
MILLER | 1300.00 |
MARTIN | 1250.00 |
WARD | 1250.00 |
ADAMS | 1100.00 |
JAMES | 950.00 |
SMITH | 800.00 |
Note: The principle of multiple fields sorting at the same time: the higher the field, the more dominant. Only when the previous field cannot be sorted, the latter field will be enabled .
select ename,sal from emp order by 2;
2 represents the second column.
by后面跟数字时表示第几列。
找出工作岗位是SALESMAN的员工,并且要求按照薪资的降序排列。
select
ename,job,sal 3
from
emp 1
where
job = 'SALESMAN' 2
order by
sal desc; 4
order by是最后执行的
select
字段 3
from
表名 1
where
条件 2
order by
.... 4
order by是最后执行的。
分组函数
分组函数?
count 计数
sum 求和
avg 平均值
max 最大值
min 最小值
记住:所有的分组函数都是对“某一组”数据进行操作的。
找出工资总和?
select sum(sal) from emp;
找出最高工资?
select max(sal) from emp;
找出最低工资?
select min(sal) from emp;
min(sal) |
---|
800.00 |
找出平均工资?
select avg(sal) from emp;
找出总人数?
select count(*) from emp;
select count(ename) from emp;
count(*) |
---|
14 |
分组函数一共5个。
分组函数还有另一个名字:多行处理函数。
多行处理函数的特点:输入多行,最终输出的结果是1行。
分组函数自动忽略NULL。
select count(comm) from emp;
count(comm) |
---|
4 |
select sum(comm) from emp;
sum(comm) |
---|
2200.00 |
单行处理函数:输入一行,输出一行
计算每个员工的年薪?
select ename,(sal+comm)*12 as yearsal from emp;
ename | yearsal |
---|---|
SMITH | NULL |
ALLEN | 22800.00 |
WARD | 21000.00 |
JONES | NULL |
MARTIN | 31800.00 |
BLAKE | NULL |
CLARK | NULL |
SCOTT | NULL |
KING | NULL |
TURNER | 18000.00 |
ADAMS | NULL |
JAMES | NULL |
FORD | NULL |
MILLER | NULL |
Smith的年薪为空,因为smith的comm为空,而数据库中有一个规定,只要数据表达式中有NULL出现,最终结果都是NULL。
重点:所有数据库都是这样规定的,只要有NULL参与的运算结果一定是NULL
ifnull() 空处理函数?
ifnull(可能为NULL的数据,被当做什么来处理):属于单行处理函数
select ename,ifnull(comm,0) as comm from emp;
ename | comm |
---|---|
SMITH | 0.00 |
ALLEN | 300.00 |
WARD | 500.00 |
JONES | 0.00 |
MARTIN | 1400.00 |
BLAKE | 0.00 |
CLARK | 0.00 |
SCOTT | 0.00 |
KING | 0.00 |
TURNER | 0.00 |
ADAMS | 0.00 |
JAMES | 0.00 |
FORD | 0.00 |
MILLER | 0.00 |
select ename,(sal+ifnull(comm,0))*12 as yearsal from emp;
ename | yearsal |
---|---|
SMITH | 9600.00 |
ALLEN | 22800.00 |
WARD | 21000.00 |
JONES | 35700.00 |
MARTIN | 31800.00 |
BLAKE | 34200.00 |
CLARK | 29400.00 |
SCOTT | 36000.00 |
KING | 60000.00 |
TURNER | 18000.00 |
ADAMS | 13200.00 |
JAMES | 11400.00 |
FORD | 36000.00 |
MILLER | 15600.00 |
select sum(comm) from emp;
sum(comm) |
---|
2200.00 |
select sum(comm) from emp where comm is not null;
不需要额外添加这个过滤条件。sum函数自动忽略NULL。
count也忽略了null
分组函数都可以直接忽略null
找出工资高于平均工资的员工?
select ename,sal from emp where sal>avg(sal);
[SQL]select ename,sal from emp where sal>avg(sal);报错[Err] 1111 - Invalid use of group function思考以上的错误信息:无效的使用了分组函数?
原因:SQL语句当中有一个语法规则,分组函数不可直接使用在where子句当中。why???
因为group by是在where执行之后才会执行的。(如果没有写group by也会自成一组,相当于有一个缺省的group by语句)
select 5
..
from 1
..
where 2
..
group by 3
..
having 4
..
order by 6
找出工资高于平均工资的员工?
第一步:找出平均工资
select avg(sal) from emp;
avg(sal) |
---|
2073.214286 |
第二步:找出高于平均工资的员工
select ename,sal from emp where sal > 2073.214286;
ename | sal |
---|---|
JONES | 2975.00 |
BLAKE | 2850.00 |
CLARK | 2450.00 |
SCOTT | 3000.00 |
KING | 5000.00 |
FORD | 3000.00 |
两条语句拼接到一起,select语句中嵌套select语句是子查询
select ename,sal from emp where sal > (select avg(sal) from emp);
count()和count(具体的某个字段),他们有什么区别?
count():不是统计某个字段中数据的个数,而是统计总记录条数。(和某个字段无关)
count(comm): 表示统计comm字段中不为NULL的数据总数量。
select count(*) from emp; //输出14
select count(comm) from emp; //输出4
分组函数也能组合起来用
select count(*),sum(sal),avg(sal),max(sal),min(sal) from emp;
count(*) | sum(sal) | avg(sal) | max(sal) | min(sal) |
---|---|---|---|---|
14 | 29025.00 | 2073.214286 | 5000.00 | 800.00 |