实验名称:实验3 数据高级查询实验 |
实验目的和要求: ·实验目的和要求 掌握SQL嵌套查询和集合查询等各种高级查询的设计方法等。 |
实验软硬件要求: 计算机、MySQL,Navicat |
实验内容、实验过程、所遇问题及其解决方法
针对TPC-H数据库,正确分析用户查询要求,设计各种嵌套查询和集合查询。
(1)IN嵌套查询法1:
SELECT custkey,name FROM customer WHERE custkey in( SELECT O.custkey FROM lineitem L,part P,orders O,partsupp PS WHERE O.orderkey=L.orderkey AND L.partkey=PS.partkey AND L.suppkey=PS.suppkey AND PS.partkey=P.partkey AND P.mgfr='赫努巴夫汽车配饰厂' AND P.name='安全带' );
法2:
SELECT custkey,name FROM customer WHERE custkey in( SELECT O.custkey FROM lineitem L,part P,orders O,customer C WHERE L.partkey=P.partkey AND
O.orderkey=L.orderkey AND O.orderkey=C.custkey AND P.mgfr='赫努巴夫汽车配饰厂' AND P.name='安全带' );
(2)单层EXSIT嵌套查询·代码SELECT custkey,name FROM customer C WHERE NOT EXISTS(SELECT O.custkey FROM lineitem L,part P,orders O,partsupp PS WHERE C.custkey=O.custkey AND O.orderkey=L.orderkey AND L.partkey=PS.partkey AND L.suppkey=PS.suppkey AND PS.partkey=P.partkey AND P.mgfr='赫努巴夫汽车配饰厂' AND P.name='安全带' );
(3)双层EXISTS嵌套查询查询至少购买过顾客“王小五”购买过的全部零件的顾客姓名。
SELECT ca.name FROM customer ca WHERE not exists (select * from customer cb,orders ob,lineitem lb where cb.custkey=ob.custkey and ob.orderkey=lb.orderkey and cb.name='王小五' and not exists(select * from orders oc,lineitem lc where ca.custkey = oc.custkey and oc.orderkey=lc.orderkey and lb.suppkey=lc.suppkey and lb.partkey=lc.partkey ) )
(4)FROM子句中的嵌套查询查询订单平均金额超过1000元的顾客中的中国籍顾客信息。 代码SELECT C.* FROM Customer C,(SELECT custkey FROM Orders GROUP BY custkey HAVING AVG(totalprice)>1000) B,nation N where C.Custkey=B.custkey and C.nationkey=N.nationkey and N.name='中国'; 结果(5)集合查询(交)查询至少购买过顾客“ 白鹭谣”和“王小五”度订购过的全部零件的信息。 ·代码SELECT P.* FROM Customer C,Orders O,Lineitem L,Partsupp PS,Part P WHERE C.custkey=o.custkey and o.orderkey=l.orderkey and l.suppkey=ps.suppkey and l.partkey=ps.partkey and ps.partkey=p.partkey and c.name='王小五'; INTERSECTion SELECT P.* FROM Customer C,Orders O,Lineitem L,Partsupp PS,Part P WHERE C.custkey=o.custkey and o.orderkey=l.orderkey and l.suppkey=ps.suppkey and l.partkey=ps.partkey and ps.partkey=p.partkey and c.name='白鹭谣';
(6)集合查询(并)查询顾客“王小五”和“白鹭谣”订购的全部零件的信息。 代码SELECT P.* FROM Customer C,Orders O,Lineitem L,Partsupp PS,Part P WHERE C.custkey=o.custkey and o.orderkey=l.orderkey and l.suppkey=ps.suppkey and l.partkey=ps.partkey and ps.partkey=p.partkey and c.name='白鹭谣'; union SELECT P.* FROM Customer C,Orders O,Lineitem L,Partsupp PS,Part P WHERE C.custkey=o.custkey and o.orderkey=l.orderkey and l.suppkey=ps.suppkey and l.partkey=ps.partkey and ps.partkey=p.partkey and c.name='王小五';
(7)集合查询(差)查询顾客“白鹭谣”订购过“王小五”订购的全部零件的信息。
SELECT P.* FROM Customer C,Orders O,Lineitem L,Partsupp PS,Part P WHERE C.custkey=o.custkey and o.orderkey=l.orderkey and l.suppkey=ps.suppkey and l.partkey=ps.partkey and ps.partkey=p.partkey and c.name='白鹭谣'; except SELECT P.* FROM Customer C,Orders O,Lineitem L,Partsupp PS,Part P WHERE C.custkey=o.custkey and o.orderkey=l.orderkey and l.suppkey=ps.suppkey and l.partkey=ps.partkey and ps.partkey=p.partkey and c.name='王小五';
解决方法:使用DISTINCT关键字去除重复记录,等关联查询方式避免数据丢失。
解决方法:将查询结果强制转换为所需的数据类型,例如使用CAST或CONVERT函数进行数据类型转换。
解决方法:使用IS NULL或IS NOT NULL操作符筛选出包含或不包含NULL值的结果。可以使用IFNULL函数将NULL值替换为指定的值。 |
实验总结及心得体会:①多表查询:在实际应用中,一个查询往往需要涉及到多个表。这时可以使用多表查询,将多个表连接起来,实现跨表查询。 ②子查询:子查询是将一个查询语句嵌套在另一个查询语句中,以实现更复杂的查询。子查询可以出现在where子句、from子句、select子句中,具体使用可以根据实际情况灵活运用。 |