sql中的遇到的有问题的
----题5:求出住在同一城市的顾客对
--select city,count(cid) as 顾客的个数
from customers group by city
--select
--select distinct
c1.cname,c2.cname,c1.city from
customers c1, customers c2 where
c1.city=c2.city and c1.cname<c2.cname
select c1.cid,c2.cid from customers
c1,customers c2
where c1.city=c2.city and
c1.cid<c2.cid
--此题的关键点即为可以给一个表取两个别
名.如果题目要求在一个表中的同一列取出
匹配项的话都可以用这种方法,如果一个城
市有三个顾客呢?
2、in和exists的问题
--题6:检索由住在Duluth的顾客和住在New York 的代理商组成的所有订货记录的ordno值
select ordno from orders where cid in (select cid from customers where city='Duluth') and aid in (select aid from agents where city='New York') --6ms
--答案:
select ordno from orders x where exists (select cid,aid from customers c,agents a
where c.cid=x.cid and a.aid=x.aid and c.city='Duluth' and a.city='New York') --10ms
--疑惑:难道in比exists执行效率高,还是只是该题的问题
--题7:找出佣金百分率最小的代理商的aid值
select top(1) aid from agents order by [percent] --我能想到的就是排序然后取第一个,但是我这样做有问题,因为我求出来的只可能有 一个,而实际情况是可能有相同值的不止一个
--答案:
select aid from agents where [percent]<=all(select [percent] from agents)
----题8:找出住在Dallas或Boston的顾客拥有相同折扣的所有顾客
--select c1.cname ,c2.cname from customers c1,customers c2 where c1.discnt=c2.discnt and c1.cid<c2.cid --该方法得出的结果跟实际不符合
----我没想出来,该怎么做?
--题9:找出与住在Dallas或Boston的顾客拥有相同折扣的所有顾客
select cid,cname from customers where discnt in (select discnt from customers where city='Dallas' or city='Boston')
--答案:
select cid,cname from customers where discnt=some(select discnt from customers where city='Dallas' or city='Boston')
--执行效率:in 3ms,some 6ms,难道in 的执行效率比some高?
--题10:求出所有满足一下条件的顾客的cid值:该顾客的discnt值小于任一住在Duluth的顾客的discnt值
select cid from customers where discnt<any(select discnt from customers where city='Duluth') --这里是错误的,题目中的任一应该是对应所有的,所以应把any改为all
--这种题目应谨慎,留意
题12:求出既订购了产品p01又订购了产品p07的顾客的cid值
--正确答案:
select distinct cid from orders x
where pid='p01' and exists (select * from orders where cid=x.cid and pid='p07')
--为什么这里一定要取别名
--取别名除了有方便的好处外,有什么情况是必须用到的吗?
select cid from orders where pid='p01' intersect select cid from orders where pid='p07'
--注:两个的交集,可以用intersect关键字
--3.4.12 检索没有通过代理商a05订货的所有顾客的名字
select cid,cname from customers where cid not in (select cid from orders where aid='a05')
--这个时候in 不能用exists 代替
--3.5.4 找出订购了产品p01和价格超过1美元的所有产品的代理商的aid值
select aid from orders where dollars/qty>1 intersect select aid from orders where pid='p01' --并且或交集的意思在SQL里面如何表达?
--
select aid from orders where pid in (select pid from products where price>1 or pid='p01' )
--这显然也是错误的,不是要它满足某个条件就行,而是要同时包含这两者
--此题没想出来
--可见,求交集的时候intersect的重要性
--答案:
select y.aid from orders y where y.pid='p01' and not exists (select p.pid from products p where p.price>1.0000 and
not exists (select * from orders x where x.pid=p.pid and x.aid=y.aid))
--3.5.5 找出具有以下性质的顾客的cid 值:如果顾客c006订购了某种产品,那要检索的顾客也订购了该产品
select cname,cid from customers where cid in (select cid from orders where pid in (select pid from orders where cid='c006'))
--跟答案不符,那么该怎么写呢?问题还是应该为包含,而不是在其中满足某个条件
--答案:
select cid from customers c where not exists (select z.pid from orders z
where z.cid='c006' and not exists (select * from orders y where y.pid=z.pid and y.cid=c.cid)
)
--3.5.6 找出被所有住在Duluth的顾客订购的产品的pid值
select distinct pid from orders where cid in (select cid from customers where city='Duluth' )
--同理:肯定是错的,对待这种要包含的问题该如何写sql语句
--答案:
select pid from products p where not exists (select c.cid from customers c where c.city='Duluth'
and not exists (select * from orders x where x.pid=p.pid and x.cid=c.cid)
)
--3.6.2 检索没有通过代理商a05订货的所有顾客的名字
select cname from customers except
(select cname from customers,orders where customers.cid=orders.cid and orders.aid='a05')
--这时except是关键
--3.6.4 检索至少订购了一件价格低于¥0.50 的商品的所有顾客的姓名
--答案:我没做出来,下面这种方法运行没通过
select distinct cname from (orders join products using(pid)) join customers using(cid) where price<0.50
--法2:将3个表直接连接起来就可以了
select distinct cname from (orders o join products p on o.pid=p.pid) join customers c on o.cid=c.cid where p.price<0.5
--3.8.2 打印出代理商的名字和标识号、产品的名字和标识号以及每个代理商为顾客c002和c003订购该产品的总量
select aname,aid,pname,pid,sum(qty) as total
select aid,pid,sum(qty) as total from orders where cid='c002' or cid=
select aid,cid,pid,sum(qty) as total from orders where cid in ('c002','c003') group by aid,cid,pid inner
select aname,orders.aid,pname,orders.pid,sum(qty) as total from orders inner join
agents on agents.aid=orders.aid inner join products on orders.pid=products.pid where
cid in ('c002','c003') group by orders.aid,orders.cid,orders.pid
--答案:
select aname,a.aid,pname,p.pid,sum(qty) as 每个代理商为每个顾客订购的该产品的总量 from orders x,products p,agents a
where x.pid=p.pid and x.aid=a.aid and x.cid in('c002','c003')
group by a.aid,aname,p.pid,p.pname
--3.8.5 构造一个查询来求出所有代理商的最大销售额平均值
select avg(select max(dollars)
from orders group by aid) from orders
--该SQL语句之所以有错误是因为基本sql不允许集合函数内部包含子查询也不允许from子句包含子查询
--但如果使用扩展语法,我们就能将子查询放在from子句中,通过对表重新命名并对集合函数生成的列命名,我们就能如愿以偿地实现上述查询
select avg(t.x) from (select aid,max(dollars) as x from orders group by aid) t
--由此可看出取别名的好处允许from子句包含子查询
--3.9.4 检索cname值以字母‘A’打头的顾客的所有信息
select * from customers where cname like 'A%'--注意:相似查询这样用
--3.9.5 检索cname值的第三个字母不等于“%”的顾客的cid值
--不知道怎么做?
select cid from customers where cname not like '__\%%' escape'\'
--3.9.6 检索cname值以'Tip_’打头并且后面跟着任意个字符的顾客的cid值
select cid,cname from customers where cname like 'Tip_%'
--答案:
select cid from customers where cname like 'Tip\_%' escape '\' --难道这种方法有错误 ?得不出答案,上面的刚好可以满足题意,但是其他题目又可以