Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350774
  • 博文数量: 88
  • 博客积分: 1673
  • 博客等级: 上尉
  • 技术积分: 934
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-20 13:51
文章分类

全部博文(88)

文章存档

2016年(1)

2015年(4)

2014年(3)

2013年(7)

2012年(11)

2011年(1)

2009年(61)

我的朋友

分类: 数据库开发技术

2009-08-02 12:56:17

  数据库查询觉得以下的这个例子就能很好的概括完整,希望对又需要学习SQL语句的朋友有帮助,我相信你把下边的例子弄懂后,再在做点其他的sql查询就已经对查询语句有所掌握:

以下面的数据库为例,用SQL完成以下检索。关系模式如下:仓库(仓库号,城市,面积)←→ WAREHOUSE(WMNO,CITY,SIZE)

职工(仓库号,职工号,工资)←→ EMPLOYEE(WHNO,ENO,SALARY)
订购单(职工号,供应商号,订购单号,订购日期)←→ ORDER(SNO,SNO,ONO,DATE)
供应商(供应商号,供应商名,地址)←→ SUPPLIER(SNO,SNAME,ADDR)
(1) 检索在北京的供应商的名称。
SELECT SNAME FROM SUPPLIER WHERE ADDR==‘北京’
(2) 检索发给供应商S6的订购单号。
SELECT ONO FROM ORDER WHERE SNO=‘S6’
(3) 检索出职工E6发给供应商S6的订购单号。
SELECT ONO FROM ORDER WHERE ENO=‘E6’ AND SNO=‘S6’ 
(4) 检索出向供应商S3发过订购单的职工的职工号和仓库号。
SELECT ENO,WHNO FROM EMPLOYEE  
WHERE ENO IN  
  (SELECT ENO  
  FROM ORDER  
  WHERE SNO=‘S3’)
或:SELECT ENO,WHNO FROM EMPLOYEE JOIN ORDER ON MPLOYEE.ENO=ORDER.ENO
  WHERE ORDER.SNO=‘S3’
(5) 检索出目前与S3供应商没有联系的职工信息。
SELECT ENO,WHNO  
FROM EMPLOYEE
WHERE ENO NOT IN  
(SELECT ENO FROM ORDER  
  WHERE SNO=‘S3’)
(6) 检索出目前没有任何订购单的供应商信息。
SELECT * FROM SUPPLIER  
WHERE SNO NOT IN  
(SELECT SNO FROM ORDER)
(7) 检索出和职工E1、E3都有联系的北京的供应商信息。
SELECT * FROM SUPPLIER  
WHERE ADDR=‘北京’ AND  
(EXISTS (SELECT * FROM ORDER WHERE SNO=SUPPIER.SNO AND ENO=‘E3’))
 AND (EXISTS (SELECT * FROM ORDER WHERE SNO=SUPPIER.SNO AND ENO=‘E6’))
(8) 检索出目前和华通电子公司有业务联系的每个职工的工资。
SELECT ENO,SALARY FROM EMPLOYEE  
WHERE ENO IN  
(SELECT ENO FROM ORDER WHERE SNO IN  
(SELECT SNO FROM SUPPLIER WHERE ADDR=‘华通电子公司’))
(9) 检索出与工资在1220元以下的职工没有联系的供应商的名称。
SELECT SNAME FROM SUPPLIER  
WHERE SNO NOT IN  
(SELECT SNO FROM ORDER WHERE ENO IN  
(SELECT ENO FROM EMPLOYEE WHERE SALARY<1220))
(10) 检索出向S4供应商发出订购单的仓库所在的城市。
SELECT CITY FROM WAREHOUSE
WHERE WHNO IN 
(SELECT WHNO FROM EMPLOYEE  
WHERE ENO IN 
(SELECT ENO FROM ORDER WHERE SNO=‘S4’));
或:
SELECT CITY FROM WAREHOUSE
JOIN EMPLOYEE ON WAREHOUSE.WHNO=EMPLOYEE.WHNO
JOIN ORDER ON EMPLOYEE.ENO=ORDER.ENO
WHERE ORDER.SNO=‘S4’
 (11) 检索出在上海工作并且向S6供应商发出了订购单的职工号。
SELECT ENO FROM EMPLOYEE  
WHERE WHNO IN  
(SELECT WHNO FROM WAREHOUSE WHERE CITY=‘上海’)
AND (ENO IN SELECT ENO FROM ORDER WHERE SNO=‘S6’);
或:
SELECT ENO FROM EMPLOYEE
JOIN WAREHOUSE ON EMPLOYEE.WHNO=WREHOUSE.WHNO
JOIN ORDER ON EMPLOYEE.ENO=ORDER.ENO
WHERE WREHOUSE.CITY=‘上海’ AND ORDER.SNO=‘S6’ 
(12) 检索出在广州工作并且只向S6供应商发出了订购单的职工号。
SELECT ENO FROM EMPLOYEE
WHERE WHNO IN (SELECT WHNO FROM WAREHOUSE WHERE CITY=‘广州’)
AND ENO IN (SELECT ENO FROM ORDER WHERE SNO=‘S6’)
AND (NOT EXISTS (SELECT * FROM ORDER  
WHERE SNO<>‘S6’ AND ENO=EMPLOYEE.ENO)) 
(13) 检索出由工资多于1230元的职工向北京的供应商发出的订购单号。
SELECT ONO FROM ORDER  
WHERE ENO IN (SELECT ENO FROM EMPLOYEE WHERE SALARY>1230)
  AND SNO IN (SELECT SNO FROM SUPPLIER WHERE ADDR=‘北京’)
或:
SELECT ONO FROM ORDER
JOIN EMPLOYEE ON ORDER.ENO= EMPLOYEE.ENO 
JOIN SUPPLIER ON ORDER.SNO= SUPPLIER.SNO 
WHERE EMPLOYEE.SALARY>1230
  AND SUPPLIER. ADDR=‘北京’
(14) 检索出仓库的个数。
SELECT COUNT(*) FROM WAREHOUSE
(15) 检索出有最大面积的仓库信息。
SELECT *  
FROM WAREHOUSE OUTER
WHERE OUTER.SIZE=(SELECT MAX(SIZE) FROM WAREHOUSE INNER)
(16) 检索出所有仓库的平均面积。
SELECT AVG(SIZE) FROM WAREHOUSE
(17) 检索出向S4供应商发出订购单的那些仓库的平均面积。
SELECT AVG(SIZE) FROM WAREHOUSE 
WHERE WHNO IN 
(SELECT WHNO FROM EMPLOYEE
WHERE ENO IN 
(SELECT ENO FROM ORDER WHERE SNO=‘S4’))
(18) 检索出每个城市的供应商个数。
SELECT CITY,COUNT(SNO)  
FROM SUPPLIER  
GROUP BY CITY
(19) 检索出每个仓库中工资多于1220元的职工个数。
SELECT WHNO,COUNT(ENO)  
FROM EMPLOYEE  
WHERE SALARY>1220  
GROUP BY WHNO
或:SELECT WHNO,COUNT(ENO)  
FROM EMPLOYEE 
GROUP BY WHNO  
HAVING SALARY>1220
(20) 检索出和面积最小的仓库有联系的供应商的个数。
SELECT COUNT(DISTINCT SNO) FROM ORDER
WHERE ENO IN  
(SELECT ENO FROM EMPLOYEE 
 WHERE WHNO IN  
(SELECT WHNO FROM WAREHOUSE OUTER
WHERE OUTER.SIZE=SELECT MIN(SIZE) 
FROM WAREHOUSE INNER))
(21) 检索出工资低于本仓库平均工资的职工信息。
SELECT * FROM EMPLOYEE OUTER
WHERE OUTER.SALARY < (SELECT AVG(SALARY) FROM EMPLOYEE INNER
WHERE INNER.WHNO=OUTER.WHNO GROUP BY WHNO)



阅读(4040) | 评论(0) | 转发(0) |
0

上一篇: select查询

下一篇:Spring的两大核心

给主人留下些什么吧!~~