1.我有一个table:COMPUTER_PRICE,格式如下:
goods price dates
HP电脑 20000 5.21
HP电脑 20050 5.23
NEC电脑 31200 5.3
NEC电脑 32000 5.5
查询结果要求:要查出每种电脑的最新价格;
上面表的结果为:
goods price dates
HP电脑 20050 5.23
可以将子查询(as subquery)或in或exists当成where的一个条件的一部分,这样的查询称为子查询
.where中可以包含一个select语句的子查询
UNION: 可以将两个以上的表的相类似的查询结果放在一起 (union all则表示返回所有的行)
具体语法:
select ...
union[all]
select...
==========
INTERSECT: 返回两个表中相同的信息
具体语法:
select ...
intersect
select...
==========
MINUS : 返回一个表中出现的信息
具体语法:
select ...
minus
select...
goods price dates
HP电脑 20000 5.21
HP电脑 20050 5.23
NEC电脑 31200 5.3
NEC电脑 32000 5.5
查询结果要求:要查出每种电脑的最新价格;
上面表的结果为:
goods price dates
HP电脑 20050 5.23
NEC电脑 32000 5.5
select goods,price,dates from COMPUTER_PRICE where dates=(select max(dates) from computer_price group by goods);
select goods,price,dates from computer_price where dates in(select max(dates) from computer_price group by goods)
select goods,price,dates from computer_price where (goods,dates)=(select goods,max(dates) from computer_price group by goods)
1.1相关子查询可以将子查询(as subquery)或in或exists当成where的一个条件的一部分,这样的查询称为子查询
.where中可以包含一个select语句的子查询
.where中可以包含in,exists语句
1.2外连接
select a.* , b.skill from students a,student_skill b where a.st_id=b.st_id
order by a.st_id;
1.3自我连接
select e1.ename ||' work for '|| e2.ename "Employees and their Managers"
from scott.emp e1,scott.emp e2 where e1.mgr=e2.empno;
1.4UNION , INTERSECT及 MINUSUNION: 可以将两个以上的表的相类似的查询结果放在一起 (union all则表示返回所有的行)
具体语法:
select ...
union[all]
select...
==========
INTERSECT: 返回两个表中相同的信息
具体语法:
select ...
intersect
select...
==========
MINUS : 返回一个表中出现的信息
具体语法:
select ...
minus
select...
select st_id from students
union
select st_id from student_skill;
列出有特长的学生的学号
select st_id from students
intersect
select st_id from student_skill;
列出没有特长学生的学号
select st_id from students
minus
select st_id from student_skill;