在select后面嵌套子查询
案例:找出每个员工所在的部门名称,要求显示员工名和部门名。
select e.ename,d.dname from emp e join dept d on e.deptno=d.deptno;//连接查询
select e.ename,(select d.dname from dept d where d.deptno=e.deptno) as dname from emp e;//子查询
注意:在使用union的时候,列的数量必须一样
limit: 重点中的重点,以后分页全靠它了。
语法机制: srartIndex(起始位置,从0开始,0表示第一条数据),length(取几个)
案例:取出工资前5名的员工(思路:工资降序取前5个)
select ename,sal from emp order by sal desc limit 0,5;
select ename,sal from emp order by sal desc limit 5;
limit是sql语句最后执行的一个环节
案例:找出工资排名在第4到第9的员工
select ename,sal from emp order by sal desc limit 3,6;
重点:怎么快速删除大表中的数据(截断表,不可回滚,永久丢失)
truncate table emp1;
重点:
重点
duplicate 重复
第一道题:
select e.ename,t. from (select deptno,max(sal) as maxsal from emp group by deptno) t join emp e on t.deptno=e.deptno and t.maxsal=e.sal;*
第一道题结束