一、多表查询
(一)简单多表查询
SELECT * FROM emp; --14条记录 SELECT * FROM dept;--4条记录 SELECT * FROM emp,dept;--显示56条数据??为什么
SELECT a.ename,a.sal,b.dname FROM emp a ,dept b WHERE a.deptno = b.deptno;
SELECT a.ename,a.sal,b.dname FROM emp a ,dept b WHERE a.deptno = b.deptno and a.deptno=10;
SELECT a.ename,a.sal,b.grade FROM emp a, salgrade b WHERE a.sal between b.losal and b.hisal;
SELECT a.ename,a.sal,b.dname FROM emp a ,dept b WHERE a.deptno = b.deptno order by b.dname;
(二)自连接
1、含义:自连接是指在同一张表的连接查询,即把一张表看成 2 张表。
SELECT a.* FROM emp a WHERE a.empno = (SELECT a.mgr FROM emp a WHERE a.ename = 'FORD') ;分析:
SELECT distinct a.ename,b.ename,b.* FROM emp a, emp b WHERE a.mgr = b.empno;--13条数据 SELECT distinct a.ename,b.ename,b.* FROM emp a, emp b WHERE a.mgr = b.empno(+);--14条数据
(三)子查询*
SELECT a.* FROM emp a WHERE a.deptno = (SELECT a.deptno FROM emp a WHERE a.ename='SMITH' );
SELECT a.ename,a.job,a.sal,a.deptno FROM emp a WHERE a.job in (SELECT distinct a.job FROM emp a WHERE a.deptno = 10) ;
SELECT a.* FROM emp a WHERE a.sal > (SELECT max(a.sal) FROM emp a WHERE a.deptno=30);
SELECT a.* FROM emp a WHERE a.sal >all (SELECT a.sal FROM emp a WHERE a.deptno=30);
SELECT a.* FROM emp a WHERE a.sal > (SELECT min(a.sal) FROM emp a WHERE a.deptno=30);
SELECT a.* FROM emp a WHERE a.sal >any (SELECT a.sal FROM emp a WHERE a.deptno=30);
SELECT a.* FROM emp a WHERE (a.deptno,a.job) = (SELECT a.deptno,a.job FROM emp a WHERE a.ename='SMITH' ); SELECT a.* FROM emp a WHERE (a.deptno,a.job) in (SELECT a.deptno,a.job FROM emp a WHERE a.ename='SMITH' );
SELECT a.ename,a.sal,b.avg_sal,a.deptno FROM emp a ,(SELECT avg(a.sal) as avg_sal,a.deptno as deptno FROM emp a group by a.deptno) b WHERE a.deptno = b.deptno and a.sal > b.avg_sal;
SELECT a.ename,a.sal,t.max_sal , a.deptno FROM emp a, (SELECT max(a.sal) as max_sal,a.deptno as deptno FROM emp a group by a.deptno) t WHERE a.deptno = t.deptno and a.sal = t.max_sal;
SELECT a.* FROM emp a WHERE a.sal = (SELECT max(b.sal) FROM emp b WHERE b.deptno = a.deptno ) ;
SELECT a.*,t.count_empno FROM dept a, (SELECT count(a.empno) as count_empno,a.deptno FROM emp a group by a.deptno) t WHERE a.deptno = t.deptno(+);
create table temp# as select empno, ename from emp where ename like 'J%';
insert into myTest (empno,ename,job) select empno,ename,job from myTest;
update emp set (empno,ename,job) = (select empno,ename,job from emp where ename = 'SMITH') where ename='SCOTT';
(四)合并查询
SELECT a.* FROM emp a WHERE a.sal> 2500 UNION SELECT b.* FROM emp b WHERE b.job='MANAGER';
SELECT a.* FROM emp a WHERE a.sal> 2500 UNION ALL SELECT b.* FROM emp b WHERE b.job='MANAGER';
2)没有交集的情况:
二、分页
(一)其他数据库的分页
1、MySQL
1)语法:
select * from 表名 where 条件 limit 从第几条,取几条 select * from emp where empno=1234 limit 1,3;
select top 2 * from 表名 where id not in (select top 4 id from 表名 where 条件) 排除前4条,再取2条,实际上是取5-6条。
SELECT rownum, emp.* FROM emp where rownum <6; SELECT * FROM (SELECT t.*, rownum num FROM (SELECT * FROM emp) t WHERE rownum < 10) t2 WHERE t2.num > 4;
SELECT * FROM (SELECT t.*, rownum num FROM (SELECT a.* FROM emp a order by a.hiredate ) t WHERE rownum < 10) t2 WHERE t2.num > 7;
create table myTest as select empno,ename,job from emp;
insert into myTest (empno,ename,job) select empno,ename,job from myTest;
SELECT * FROM (SELECT t.*, rownum num FROM (SELECT * FROM myTest ) t WHERE rownum < 51010) t2 WHERE t2.num > 50997;
三、内、外连接
SELECT a.ename,a.deptno,b.dname FROM emp a,dept b WHERE a.deptno = b.deptno;2)【inner】 Join on
SELECT a.ename,a.deptno,b.dname FROM emp a inner join dept b on (a.deptno = b.deptno)
SELECT a.id,a.name,b.grade FROM stu a,exam b WHERE a.id = b.id;
SELECT a.id,a.name,b.grade FROM stu a,exam b WHERE a.id = b.id(+); SELECT a.id,a.name,b.grade FROM stu a left join exam b on( a.id = b.id);
SELECT a.id,a.name,b.grade FROM stu a,exam b WHERE a.id(+) = b.id; SELECT a.id,a.name,b.grade FROM stu a right join exam b on( a.id = b.id);
SELECT a.id,a.name,b.grade FROM stu a full join exam b on( a.id = b.id); SELECT a.id,a.name,b.grade FROM stu a full outer join exam b on( a.id = b.id);
SELECT a.id,a.name,b.grade FROM stu a right join exam b on(a.id = b.id); SELECT a.id,a.name,b.grade FROM exam b left join stu a on( a.id = b.id);
6、练习:
1)列出部门名称和这些部门员工的员工信息,同时列出那些没有员工的部门
SELECT b.dname,a.* FROM emp a right join dept b on (a.deptno = b.deptno) order by b.deptno; SELECT b.dname,a.* FROM dept b left join emp a on (a.deptno = b.deptno) order by b.deptno; SELECT b.dname,a.* FROM emp a, dept b WHERE b.deptno = a.deptno(+) order by b.deptno; SELECT b.dname,a.* FROM emp a, dept b WHERE a.deptno(+) = b.deptno order by b.deptno;
四、练习
<span style="font-family:Arial;font-size:18px;">SELECT count(*) ,count(comm) FROM emp;</span>
SELECT count(distinct mgr) FROM emp;
<span style="font-family:Arial;font-size:18px;">SELECT avg(a.sal),a.deptno,a.job FROM emp a GROUP BY cube(a.deptno,a.job );</span>
原文地址:http://blog.csdn.net/u012228718/article/details/46311053