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 '\' --難道這種方法有錯誤 ?得不出答案,上面的剛好可以滿足題意,但是其他題目又可以
轉載于:https://www.cnblogs.com/chenwancoco/archive/2011/08/12/2135990.html
總結
以上是生活随笔為你收集整理的sql中的遇到的有问题的的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++资源库不完全版本
- 下一篇: Oracle数据库卸载