-- 创建员工表
create table emp(
empno int primary key auto_increment,
ename varchar(50) not null,
job varchar(50),
mgr int,
hiredate date,
sal decimal(7,2),-- 7 保留7位有效数字,2精确到小数点后2位
comm decimal(7,2),
deptno int
);
-- 填充员工信息
INSERT INTO emp values(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO emp values(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO emp values(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO emp values(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp values(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO emp values(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO emp values(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);
INSERT INTO emp values(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp values(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO emp values(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);
INSERT INTO emp values(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO emp values(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO emp values(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);
-- 4.字段控制查询
-- 4.1 去除重复记录:去重sal
select distinct sal from emp;
-- 4.2 查看雇员的月薪与佣金之和
-- IFNULL(comm,0) 如果comm字段对应的当前值为null就将该值赋为0,不为null则用对应值
select empno,ename,sal,comm,sal+ifnull(comm,0) as '工资'
from emp
-- 4.3 给列名添加别名
-- sal+ifnull(comm,0),给这一列给出一个别名,为total:
select *, sal + ifnull(comm,0) as total from emp;
SELECT *, sal + IFNULL(comm,0) total FROM emp;-- as可以省略
-- 5 排序
-- 5.1 查询所有学生记录,按年龄升序排序
SELECT sid,sname,age,gender
FROM stu
order by age asc;
-- 5.2 查询所有学生记录,按年龄降序排序
SELECT sid,sname,age,gender
FROM stu
order by age desc;
-- 5.3 查询所有雇员,按月薪降序排序,如果月薪相同时,按编号升序排序
SELECT empno,ename,job,hiredate,sal,comm,deptno
FROM emp
order by sal desc,empno asc;
select empno,ename,job,hiredate,sal,comm,deptno
from emp
-- 6.1 COUNT 当需要纵向统计时可以使用COUNT().
-- 查询emp表中记录数:
select count(empno) from emp;
-- 6.2 SUM和AVG当需要纵向求和时使用sum()函数。
-- 查询emp中有佣金的人数:
select count(comm) com from emp;
-- 注意,因为count()函数中给出的是comm列,那么只统计comm列非null的行数
-- 查询emp表中月薪大于2500的人数:
select count(empno) from emp where sal>'2500';
-- 统计月薪与佣金之和大于2500元的人数:
SELECT COUNT(empno) FROM emp WHERE (sal+ifnull(comm,0))>'2500';
-- 查询有佣金的人数,以及有领导的人数:
select count(empno)
from emp where comm is not null and mgr is not null
-- 6.2 SUM和AVG当需要纵向求和时使用sum()函数。
-- 查询所有雇员月薪和:
select sum(sal) from emp;
-- 查询所有雇员月薪和,以及所有雇员佣金和:
select sum(sal),sum(somm) from emp;
-- 查询所有雇员月薪+佣金和:
select sum(sal+ifnull(comm,0)) from emp;
-- 统计所有员工平均工资:
select avg(sal) from emp;
-- 6.3 MAX和MIN
-- 查询最高工资和最低工资:
select min(sal) from emp;
SELECT Max(sal) FROM emp;
-- 7 分组查询
-- 7.1 分组查询
-- 查询每个部门的部门编号和每个部门的工资和:
select deptno,sum(sal) from emp group by deptno;
-- 查询每个部门的部门编号以及每个部门的人数:
select deptno,count(empno) from emp group by deptno;
-- 查询每个部门的部门编号以及每个部门工资大于1500的人数:
select deptno,count(empno) from emp where sal>1500 group by deptno;
-- 7.2 HAVING子句
-- 查询工资总和大于9000的部门编号以及工资和:
select deptno,sum(sal) total from emp group by deptno having total>9000;
CREATE TABLE stu(
sid CHAR(6),
sname VARCHAR(50),
age INT,
gender VARCHAR(50)
);
INSERT INTO stu VALUES('S_1001', 'liuYi', 35, 'male');
INSERT INTO stu VALUES('S_1002', 'chenEr', 15, 'female');
INSERT INTO stu VALUES('S_1003', 'zhangSan', 95, 'male');
INSERT INTO stu VALUES('S_1004', 'liSi', 65, 'female');
INSERT INTO stu VALUES('S_1005', 'wangWu', 55, 'male');
INSERT INTO stu VALUES('S_1006', 'zhaoLiu', 75, 'female');
INSERT INTO stu VALUES('S_1007', 'sunQi', 25, 'male');
INSERT INTO stu VALUES('S_1008', 'zhouBa', 45, 'female');
INSERT INTO stu VALUES('S_1009', 'wuJiu', 85, 'male');
INSERT INTO stu VALUES('S_1010', 'zhengShi', 5, 'female');
INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);
-- 1.1 查询所有列
-- 1.2 查询指定列
-- 2.2 查询性别为女,并且年龄50的记录
SELECT sid,sname,age,gender
FROM stu
WHERE gender='female' AND age<='50';
-- 2.3 查询学号为S_1001,或者姓名为liSi的记录
SELECT sid,sname,age,gender
FROM stu
WHERE sid='s_1001' OR sname='lisi';
-- 2.4 查询学号为S_1001,S_1002,S_1003的记录
SELECT sid,sname,age,gender
FROM stu
WHERE sid='s_1001' OR sid='s_1002' OR sid='s_1003';
-- 2.5 查询学号不是S_1001,S_1002,S_1003的记录
SELECT sid,sname,age,gender
FROM stu
WHERE NOT( sid='s_1001' OR sid='s_1002' OR sid='s_1003');
-- 2.6 查询年龄为null的记录
SELECT sid,sname,age,gender
FROM stu
WHERE age IS NULL;
-- 2.7 查询年龄在20到40之间的学生记录
SELECT sid,sname,age,gender
FROM stu
WHERE age>='20' AND age<='40';
-- 2.8 查询性别非男的学生记录
SELECT sid,sname,age,gender
FROM stu
WHERE gender ;
-- 2.9 查询姓名不为null的学生记录
SELECT sid,sname,age,gender
FROM stu
WHERE sname IS NOT NULL;
-- 或
SELECT sid,sname,age,gender
FROM stu
WHERE NOT sname IS NULL;
-- 3 模糊查询
-- 3.1 查询姓名由5个字母构成的学生记录
SELECT sid,sname,age,gender
FROM stu
WHERE sname LIKE '_____';
-- 3.2 查询姓名由5个字母构成,并且第5个字母为“i”的学生记录
SELECT sid,sname,age,gender
FROM stu
WHERE sname LIKE '____i';
-- 3.3 查询姓名以“z”开头的学生记录
SELECT sid,sname,age,gender
FROM stu
WHERE sname LIKE 'z%';
-- 3.4 查询姓名中第2个字母为“i”的学生记录
SELECT sid,sname,age,gender
FROM stu
WHERE sname LIKE '_i%';
-- 3.5 查询姓名中包含“a”字母的学生记录
SELECT sid,sname,age,gender
FROM stu
WHERE sname LIKE '%a%';