MySQL基础语法 2
-
排序 order by
--默认是升序 -- 单个字段排序 select * from student order by stu_no; select * from student order by stu_no asc select * from student order by stu_no desc -- 多字段排序 -- 按学号升序,按年龄降序排列 select * from student order by stu_no,stu_age desc
-
限制取记录数 limit n,m
n是offset起始点,就是从哪里取,下表从0开始
m是每一次最大取多少条
[n,n+m)
-- 取第5-10条
select * from student limit 5 , 10
-
学习SQL
初始化脚本
--部门表 -- dept部门表(deptno部门编号/dname部门名称/loc地点) create table dept ( deptno numeric(2), dname varchar(14), loc varchar(13) ); insert into dept values (10, 'ACCOUNTING', 'NEW YORK'); insert into dept values (20, 'RESEARCH', 'DALLAS'); insert into dept values (30, 'SALES', 'CHICAGO'); insert into dept values (40, 'OPERATIONS', 'BOSTON'); -- 工资等级表 -- salgrade工资等级表(grade 等级/losal此等级的最低/hisal此等级的最高) create table salgrade ( grade numeric, losal numeric, hisal numeric ); insert into salgrade values (1, 700, 1200); insert into salgrade values (2, 1201, 1400); insert into salgrade values (3, 1401, 2000); insert into salgrade values (4, 2001, 3000); insert into salgrade values (5, 3001, 9999); -- 员工表 --emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号) -- 工资 = 薪金 + 佣金 -- 1.表自己跟自己连接 create table emp ( empno numeric(4) not null, ename varchar(10), job varchar(9), mgr numeric(4), hiredate datetime, sal numeric(7, 2), comm numeric(7, 2), deptno numeric(2) ); insert into emp values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20); 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, '1982-12-09', 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, '1983-01-12', 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);
-
1.求员工表所有人的薪水和
SELECT SUM(IFNULL(sal, 0)) sum_salary FROM emp
-
2.求员工表的各个部门的薪水和
SELECT deptno, SUM(IFNULL(sal, 0)) sum_salary FROM emp GROUP BY deptno;
-
3.求员工表的各个部门的薪水和、员工数、平均薪资
SELECT deptno, SUM( IFNULL(sal, 0) + IFNULL(comm, 0) ) sum_salary_comm, COUNT(DISTINCT empno) count_emp, AVG(IFNULL(sal, 0)) avg_salary FROM emp GROUP BY deptno;
-
找薪水和>9000的是哪个部门
SELECT deptno, SUM( IFNULL(sal, 0) + IFNULL(comm, 0) ) sum_salary_comm FROM emp GROUP BY deptno HAVING sum_salary_comm > 9000;
-
多表联查 JOIN
-
LEFT JOIN
以左表为主表,左表有的,如果右表不存在,则右边的列为NULL所有数据以主表为准
SELECT emp.*,dept.dname dept_name FROM emp LEFT JOIN dept ON dept.deptno = emp.deptno
-
RIGHT JOIN
右表为主表,右表有的,如果左表不存在,则左边的列为NULL
SELECT * FROM emp RIGHT JOIN dept ON dept.deptno = emp.deptno
-
全连接(类似oracle FULL JOIN)
SELECT * FROM emp LEFT JOIN dept ON dept.deptno = emp.deptno UNION SELECT * FROM emp RIGHT JOIN dept ON dept.deptno = emp.deptno
关于左右关联的图示,可以参考这个图示,网友们分享的
-
-
Top N问题
- 每个部门薪水前2的员工信息
SELECT * FROM emp m WHERE ( SELECT COUNT(*) FROM emp s WHERE m.deptno = s.deptno AND s.sal > m.sal ) < 2 ORDER BY deptno,sal desc;
这个查询比较复杂
-
这个查询首先select * from emp m了,把所有的emp数据查询出来,
-
再遍历查询出来的每一条记录,利用where语句筛选出符合条件的,如果发现某一条记录进行关联查询之后,发现比他薪水高的记录数>=2,说明他不是前2的,就排除掉了
这个自己理解比较费劲,参考了网上的九人稚。
-
-
练习题目
- 查询出部门编号为30的所有员工的编号和姓名
SELECT * FROM emp WHERE deptno=30;
- 找出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料。
SELECT * FROM emp WHERE (deptno = 20 AND mgr IS NOT NULL) OR (deptno = 10 AND mgr IS NULL);
- 查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序
SELECT * FROM emp ORDER BY sal DESC,hiredate
- 列出最低薪金大于1500的各种工作及从事此工作的员工人数。
SELECT job, COUNT(*), MIN(sal) FROM emp WHERE job IN ( SELECT DISTINCT job HAVING MIN(sal) > 1500 );
- 列出在销售部工作的员工的姓名,假定不知道销售部的部门编号。
SELECT ename FROM emp WHERE deptno = ( SELECT deptno FROM dept WHERE dept.dname = 'SALES' );
- 查询姓名以S开头的\以S结尾\包含S字符\第二个字母为L
SELECT * FROM emp WHERE ename LIKE 'S%'; SELECT * FROM emp WHERE ename LIKE '%S'; SELECT * FROM emp WHERE ename LIKE '%S%'; SELECT * FROM emp WHERE ename LIKE '_L%';
- 查询每种工作的最高工资、最低工资、人数
SELECT job, MAX(sal) max_salary, MIN(sal) min_salary, COUNT(DISTINCT empno) FROM emp GROUP BY job;
- 列出薪金 高于 公司平均薪金的所有员工号,员工姓名,所在部门名称,上级领导,工资,工资等级
SELECT emp.empno, emp.ename, dept.dname, emp.mgr, emp.sal, salgrade.grade FROM emp LEFT JOIN dept ON emp.deptno = dept.deptno LEFT JOIN salgrade ON IFNULL(emp.sal, 0) BETWEEN salgrade.losal AND salgrade.hisal WHERE emp.sal > (SELECT AVG(emp.sal) FROM emp)
- 列出薪金 高于 在部门30工作的 所有/任何一个员工的薪金的员工姓名和薪金、部门名称。
SELECT * FROM emp WHERE sal > ( SELECT MAX(sal) FROM emp WHERE deptno = 30 ) AND deptno != 30;