子查询
1。select ... where 列或运算式 比较运算运算【any|all](子查询)
只要主查询中列或运算式与子查询所得结果中任一(any)或全部(all)数据符合比较条件的话则主查询的结果为我们要的数据
选出不同的人金额最高的订单
select * from sales a
where tomat=(select max(totmat) from sales where name=a.name)
select sale_id,tot_amt
from sales
where tot_amt>any(select tot_amt from sales where sale_id='e0013'and 'order_date='1996/11/10')
2。select ...where 列或运算式[not] in (子查询)
只要主查询中列或运算式是在(不在)子查询所得结果列表中的话,则主查询的结果为我们要的数据
select sales_id,tot_amt
from sales
where sale _id in(select sale_id from employee where sex='F')
3.select ...where [not] exists ( 子查询)
子查询的结果至少存在一条数据时,则主查询的结果为我们要的数据。(exists)或自查询的结果找不到数据时,则主查询的结果为我们要的数据(not exists)
我们经常查询的两个表有多少重复的记录就用这个
以下范例让你找出滞销的产品,也就是尚未有任何销售记录的库存产品。此范例主要是查询以库文件中的每一条产品代码到销售明细表中去查询,如果查询不到任何一条,表示该产品未曾卖出任何一件。
select * from stock a
where not exists(select * from sale_item b
where a.prod_id=b.prod_id and a.stup_id=b.stup_id)
select emp_no,emp_name from employee
from employee a
where (select sum(tot_amt) from sales b where a.sale_id=b.emp_no)<200000
子查询
原创文章如转载,请注明:转载自悠悠博客 [ http://www.ajaxstu.com/ ]
相关文章:
- join 方式(2007-11-11 8:59:26)
- 堵死SQLServer注入漏洞(2007-9-8 1:27:12)
- SQLServer性能分析(2007-7-27 7:39:49)
- Transact_SQL索引(2007-7-20 8:14:19)
- SQLServer和XML的集成(2007-7-1 9:21:28)
- SQLServer的安全性问题(结合iis和.net)(2007-6-24 1:19:48)
- SQL Server中的单引号(2007-6-11 1:9:7)
- 数据分组(2007-6-9 4:53:2)
- distinct id的取法(2007-5-7 7:20:54)
- 模糊、统计、网状、演绎数据库内容小结(2007-4-18 7:20:33)
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
