sql中的遇到的有问题的
----題5:求出住在同一城市的顧客對(duì)
--select city,count(cid) as 顧客的個(gè)數(shù)
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
--此題的關(guān)鍵點(diǎn)即為可以給一個(gè)表取兩個(gè)別
名.如果題目要求在一個(gè)表中的同一列取出
匹配項(xiàng)的話(huà)都可以用這種方法,如果一個(gè)城
市有三個(gè)顧客呢?
2、in和exists的問(wèn)題
--題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執(zhí)行效率高,還是只是該題的問(wèn)題
--題7:找出傭金百分率最小的代理商的aid值
select top(1) aid from agents order by [percent]? --我能想到的就是排序然后取第一個(gè),但是我這樣做有問(wèn)題,因?yàn)槲仪蟪鰜?lái)的只可能有 一個(gè),而實(shí)際情況是可能有相同值的不止一個(gè)
--答案:
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 --該方法得出的結(jié)果跟實(shí)際不符合
----我沒(méi)想出來(lái),該怎么做?
--題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')
--執(zhí)行效率:in 3ms,some 6ms,難道in 的執(zhí)行效率比some高?
--題10:求出所有滿(mǎn)足一下條件的顧客的cid值:該顧客的discnt值小于任一住在Duluth的顧客的discnt值
select cid from customers where discnt<any(select discnt from customers where city='Duluth') --這里是錯(cuò)誤的,題目中的任一應(yīng)該是對(duì)應(yīng)所有的,所以應(yīng)把a(bǔ)ny改為all
--這種題目應(yīng)謹(jǐn)慎,留意
題12:求出既訂購(gòu)了產(chǎn)品p01又訂購(gòu)了產(chǎn)品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'
--注:兩個(gè)的交集,可以用intersect關(guān)鍵字
--3.4.12 檢索沒(méi)有通過(guò)代理商a05訂貨的所有顧客的名字
select cid,cname from customers where cid not in (select cid from orders where aid='a05')
--這個(gè)時(shí)候in 不能用exists 代替
--3.5.4 找出訂購(gòu)了產(chǎn)品p01和價(jià)格超過(guò)1美元的所有產(chǎn)品的代理商的aid值
select aid from orders where dollars/qty>1 intersect select aid from orders where pid='p01'? --并且或交集的意思在SQL里面如何表達(dá)?
--
select aid from orders where pid in (select pid from products where price>1 or pid='p01' )
--這顯然也是錯(cuò)誤的,不是要它滿(mǎn)足某個(gè)條件就行,而是要同時(shí)包含這兩者
--此題沒(méi)想出來(lái)
--可見(jiàn),求交集的時(shí)候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 找出具有以下性質(zhì)的顧客的cid 值:如果顧客c006訂購(gòu)了某種產(chǎn)品,那要檢索的顧客也訂購(gòu)了該產(chǎn)品
select cname,cid from customers where cid in (select cid from orders where pid in (select pid from orders where cid='c006'))
--跟答案不符,那么該怎么寫(xiě)呢?問(wèn)題還是應(yīng)該為包含,而不是在其中滿(mǎn)足某個(gè)條件
--答案:
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的顧客訂購(gòu)的產(chǎn)品的pid值
select distinct pid from orders where cid in (select cid from customers where city='Duluth' )
--同理:肯定是錯(cuò)的,對(duì)待這種要包含的問(wèn)題該如何寫(xiě)sql語(yǔ)句
--答案:
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 檢索沒(méi)有通過(guò)代理商a05訂貨的所有顧客的名字
select cname from customers except
(select cname from customers,orders where customers.cid=orders.cid and orders.aid='a05')
--這時(shí)except是關(guān)鍵
--3.6.4 檢索至少訂購(gòu)了一件價(jià)格低于¥0.50 的商品的所有顧客的姓名
--答案:我沒(méi)做出來(lái),下面這種方法運(yùn)行沒(méi)通過(guò)
select distinct cname from (orders join products using(pid)) join customers using(cid) where price<0.50
--法2:將3個(gè)表直接連接起來(lái)就可以了
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 打印出代理商的名字和標(biāo)識(shí)號(hào)、產(chǎn)品的名字和標(biāo)識(shí)號(hào)以及每個(gè)代理商為顧客c002和c003訂購(gòu)該產(chǎn)品的總量
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 每個(gè)代理商為每個(gè)顧客訂購(gòu)的該產(chǎn)品的總量 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 構(gòu)造一個(gè)查詢(xún)來(lái)求出所有代理商的最大銷(xiāo)售額平均值
select avg(select max(dollars)
from orders group by aid) from orders
--該SQL語(yǔ)句之所以有錯(cuò)誤是因?yàn)榛緎ql不允許集合函數(shù)內(nèi)部包含子查詢(xún)也不允許from子句包含子查詢(xún)
--但如果使用擴(kuò)展語(yǔ)法,我們就能將子查詢(xún)放在from子句中,通過(guò)對(duì)表重新命名并對(duì)集合函數(shù)生成的列命名,我們就能如愿以?xún)數(shù)貙?shí)現(xiàn)上述查詢(xún)
select avg(t.x) from (select aid,max(dollars) as x from orders group by aid) t
--由此可看出取別名的好處允許from子句包含子查詢(xún)
--3.9.4 檢索cname值以字母‘A’打頭的顧客的所有信息
select * from customers where cname like 'A%'--注意:相似查詢(xún)這樣用
--3.9.5 檢索cname值的第三個(gè)字母不等于“%”的顧客的cid值
--不知道怎么做?
select cid from customers where cname not like '__\%%' escape'\'
--3.9.6 檢索cname值以'Tip_’打頭并且后面跟著任意個(gè)字符的顧客的cid值
select cid,cname from customers where cname like 'Tip_%'
--答案:
select cid from customers where cname like 'Tip\_%' escape '\' --難道這種方法有錯(cuò)誤 ?得不出答案,上面的剛好可以滿(mǎn)足題意,但是其他題目又可以
轉(zhuǎn)載于:https://www.cnblogs.com/chenwancoco/archive/2011/08/12/2135990.html
總結(jié)
以上是生活随笔為你收集整理的sql中的遇到的有问题的的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++资源库不完全版本
- 下一篇: 最简洁的js鼠标拖曳效果【原】