2022年sql-多表查詢-代碼示例_第1頁
2022年sql-多表查詢-代碼示例_第2頁
2022年sql-多表查詢-代碼示例_第3頁
2022年sql-多表查詢-代碼示例_第4頁
2022年sql-多表查詢-代碼示例_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、精選精選.精選. HYPERLINK :/ITFLY8/archive/2006/09/15/1224859.aspx SQL多表查詢代碼例如在Pubs數(shù)據(jù)庫中,完成以下查詢use pubs-使用內(nèi)聯(lián)接查詢出authors和publishers表中位于同一個(gè)城市的作者和出版社信息select au_fname+au_lname as 作者,pub_name as 出版社from authors inner join publisherson authors.city=publishers.city-查詢出作者號(hào)以15開頭的所有作者,并使用右外聯(lián)接在查詢的結(jié)果集中列出和作者-在同一個(gè)城市的出版社

2、名select au_fname+au_lname as 作者,pub_name as 出版社from authors right outer join publisherson authors.au_id like 1-5% where authors.city=publishers.city-使用自聯(lián)接查找居住在 Oakland 相同郵碼區(qū)域中的作者。select distinct a.au_fname+a.au_lname as 作者,a.au_id,a.zipfrom authors a inner join authors bon a.zip = b.zipwhere a.city=

3、Oakland and a.au_fname b.au_fname-P26?學(xué)習(xí)手冊(cè)?上機(jī)試驗(yàn)的所有題目select ascii(sql)-結(jié)果:115select char(66)-結(jié)果:Bselect charindex(E,HELLO)-結(jié)果:2select left(RICHARD,4)-結(jié)果:RICHselect len(RICHARD)-結(jié)果:7select lower(RICHARD)-結(jié)果:richardselect SQL+ltrim(RICHARD)-結(jié)果:SQLRICHARDselect reverse(ACTION)-結(jié)果:NOITCAselect right(RICH

4、ARD,4)-結(jié)果:HARD精選精選.精選.select rtrim(RICHARD )+SQL-結(jié)果:RICHARDSQLselect patindex(%BOX%,ACTIONBOX)-結(jié)果:7select RICHARD+space(2)+HELL-結(jié)果:RICHARD HELLselect stuff(Weather,2,2,I)-結(jié)果:WItherselect substring(Weather,2,2)-結(jié)果:easelect upper(Richard)-結(jié)果:RICHARDselect dateadd(dd,10,getdate()-結(jié)果:2005-10-26 16:04:58

5、.030select datediff(dy,getdate(),2005-01-01)-結(jié)果:-288select datepart(dw,2004-10-01)-結(jié)果:6select datename(dw,2004-10-01)-結(jié)果:星期五-第七講 多表查詢上機(jī)實(shí)驗(yàn)use recruitment- 需要得到年齡在35歲到40歲之間的外部候選人的信息select * from ExternalCandidatewhere datediff(yy,dbirthdate,getdate() between 35 and 40- 需要在當(dāng)前日期之后的10天在報(bào)紙上登載一那么廣告,系統(tǒng)需要計(jì)算出

6、日期并顯示select distinct getdate() as today,dateadd(day,10,getdate() as 10 days from todayfrom newsad- 統(tǒng)計(jì)外部候選人接受測(cè)試和面試日期的間隔的時(shí)間平均值select avg(datediff(day,dtestdate,dinterviewdate) as 測(cè)試面試日期間隔平均天數(shù)from externalcandidate- 需要獲取外部候選人的姓名和他們申請(qǐng)的職位select externalcandidate.vcandidatename as 姓名, position.vdescriptio

7、n as 申請(qǐng)職位from externalcandidate left join positionon externalcandidate.cpositioncode=position.cpositioncode- 需要獲得在2001年應(yīng)聘的外部候選人的名字,及推薦他們的招聘機(jī)構(gòu)名select externalcandidate.vcandidatename as 名字, recruitmentagencies ame as 推薦他們的招聘機(jī)構(gòu)名from externalcandidate left join recruitmentagencieson externalcandidate.c

8、agencycode=recruitmentagencies.cagencycodewhere year(externalcandidate.ddateofapplication)=2001精選精選.精選.- 需要獲取外部候選人的姓名以及他們的參照的招聘的廣告所屬的報(bào)紙名select externalcandidate.vcandidatename as 姓名, newspaper ewspapername as 參照招聘廣告所屬報(bào)紙from externalcandidate,newsad,newspaperwhere externalcandidate ewsadno=newsad ews

9、adno and newsad ewspapercode=newspaper ewspapercode- 需要獲取大學(xué)名稱、報(bào)紙名稱以及它們地址的列表select college.cCollegeName as 大學(xué)名稱,college.vcollegeaddress 學(xué)校地址, newspaper ewspapername as 報(bào)紙名稱,newspaper.vhoaddress as 報(bào)社地址from college,newspaper-問題:這兩張表之間沒有聯(lián)系,那么應(yīng)選用何種聯(lián)接?否那么這里面有太多冗余數(shù)據(jù)- 是否為同一所城市里有哪些大學(xué)和哪些報(bào)紙?select college.cCo

10、llegeName as 大學(xué)名稱,college.vcollegeaddress 學(xué)校地址, newspaper ewspapername as 報(bào)紙名稱,newspaper.vhoaddress as 報(bào)社地址from college,newspaperwhere college.ccity=newspaper.ccity-因?yàn)榇髮W(xué)所在城市的值為某某,而報(bào)紙所在城市的值為某某市,因此按此不能正確查出結(jié)果-采用以下方法可以解決select college.cCollegeName as 大學(xué)名稱,college.vcollegeaddress 學(xué)校地址, newspaper ewspaper

11、name as 報(bào)紙名稱,newspaper.vhoaddress as 報(bào)社地址from college,newspaperwhere left(ltrim(college.ccity),2)=left(ltrim(newspaper.ccity),2)-還是顯示出大學(xué)表里符合條件的記錄與報(bào)紙表里符合條件的記錄之積,內(nèi)聯(lián)接結(jié)果一樣-第七講 多表查詢作業(yè)-P26?學(xué)習(xí)手冊(cè)?上機(jī)作業(yè)的所有題目use GlobalToyz-按指定格式詳見學(xué)習(xí)手冊(cè)P27顯示所有運(yùn)貨的報(bào)表天數(shù)=實(shí)際到達(dá)日期-運(yùn)貨日期select corderno as 定單號(hào), dshipmentdate as 運(yùn)貨日期, dact

12、ualdeliverydate as 實(shí)際到達(dá)日期, datediff(day,dshipmentdate,dactualdeliverydate) as 運(yùn)送天數(shù)from shipment-小結(jié):兩日期之差運(yùn)算為第二個(gè)日期參數(shù)-第一個(gè)日期參數(shù)-按指定格式詳見學(xué)習(xí)手冊(cè)P27顯示所有的訂單select cOrderNo as 定單號(hào),cShopperId as 購物者號(hào),dOrderDate as 訂單日期號(hào), datename(dw,dorderdate)星期幾from orders精選精選.精選.-小結(jié):求星期幾,日期元素只能用DW,而不能用WK,WK求得是在一年中的第幾周,而列別名如果有-

13、 特殊字符需要引號(hào)引起來-顯示所有玩具名和所屬的種類名select toys.vToyName as 玩具名,Category.cCategory as 種類名from category join toyson toys.cCategoryId = Category.cCategoryId-小結(jié):交叉聯(lián)接不能使用條件,而內(nèi)聯(lián)接和右外聯(lián)接在此效果相同,- 左外聯(lián)接和全外聯(lián)接效果相同,但多出九條玩具名為空的記錄,- 因?yàn)樽笸饴?lián)接時(shí)將顯示所有左表中即種類表中的記錄,即使沒有該玩具屬于該種類,- 此時(shí)玩具名為NULL值- JOIN前不加關(guān)鍵字時(shí)默認(rèn)為內(nèi)聯(lián)接- 用join聯(lián)接表名時(shí),后面條件語句只能先跟

14、on關(guān)鍵字,不能直接用where-按指定格式詳見學(xué)習(xí)手冊(cè)P27顯示所有玩具的名稱、商標(biāo)和種類select toys.vtoyname as 玩具名,ToyBrand.cBrandName as 商標(biāo)名, Category.ccategory as 類別名from toys,ToyBrand,Categorywhere toys.cBrandId=ToyBrand.cBrandId and toys.cCategoryId=Category.cCategoryId-問題:如果用逗號(hào)聯(lián)系多張表,之間采用的是什么聯(lián)接方式?表與表之間的前后順序影不影響結(jié)果?-按指定格式詳見學(xué)習(xí)手冊(cè)P28顯示所有玩具的

15、訂貨號(hào)、玩具ID和玩具使用的禮品包裝說明select orderdetail.corderno as 定單號(hào),orderdetail.ctoyid as 玩具號(hào), wrapper.vdescription as 包裝信息from orderdetail left join wrapperon orderdetail.cwrapperid=wrapper.cwrapperidselect orderdetail.corderno as 定單號(hào),orderdetail.ctoyid as 玩具號(hào), wrapper.vdescription as 包裝信息from toys,orderdetail,

16、wrapperwhere toys.ctoyid=orderdetail.ctoyid and orderdetail.cwrapperid=wrapper.cwrapperid-小結(jié):外連接的關(guān)鍵字outer可以省略不寫-問題:采用以上方式查出的結(jié)果好象未能滿足需求,沒有顯示所有的玩具,如果用三張表,即- 參加toys表后,加上一個(gè)toys.ctoyid=orderdetail.ctoyid后也不能列出所有玩具。-按指定格式詳見學(xué)習(xí)手冊(cè)P28顯示所有購物者名,及他們所購置的訂單信息無論購物者是否有訂單select shopper.vfirstname as 購物者名,orders.corde

17、rno as 定單號(hào), orders.dorderdate as 定單時(shí)間,orders.mtotalcost as 定單金額from shopper left join orderson shopper.cshopperid=orders.cshopperid精選精選.精選.-按指定格式詳見學(xué)習(xí)手冊(cè)P28顯示訂單號(hào)碼、訂單日期和每個(gè)訂單所在的季節(jié)select cOrderNo as 定單號(hào),dOrderDate as 定單日期,datename(qq,dOrderDate) as 季節(jié)from orders-問題:如果要顯示季節(jié),是否需要用到分支選擇語句?-按指定格式詳見學(xué)習(xí)手冊(cè)P28顯示所

18、有購物者ID、名字、 和相應(yīng)訂單的接受者select shopper.cshopperid as 購物者號(hào),shopper.vfirstname as 名字, shopper.cphone as ,recipient.vfirstname as 接受者名,recipient.cphone as from shopper,orders,recipientwhere shopper.cshopperid = orders.cshopperid and orders.corderno = recipient.corderno-小結(jié):如果表與表之間聯(lián)接沒用JOIN,那么條件語句關(guān)鍵字不能用ON,只能用WHERE-按指定格式詳見學(xué)習(xí)手冊(cè)P28顯示所有購物者和接受者的名字、地址select shopper.vfirstname as 購物者名字,shopper.vaddress as 購物者地址, recipient.vfirstname as 接受者名字,rec

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論