sql 多表查询 代码示例_第1页
sql 多表查询 代码示例_第2页
sql 多表查询 代码示例_第3页
sql 多表查询 代码示例_第4页
sql 多表查询 代码示例_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SQL多表查询代码示例在Pubs数据库中,完成以下查询usepubs-—使用内联接查询出authors和publishers表中位于同一个城市的作者和出版社信息selectau_fname+'·’+au_lnameas作者,pub_nameas出版社fromauthorsinnerjoinpublishersonauthors.city=publishers。city—-查询出作者号以1~5开头的所有作者,并使用右外联接在查询的结果集中列出和作者—-在同一个城市的出版社名selectau_fname+'·’+au_lnameas作者,pub_nameas出版社fromauthorsrightouterjoinpublishersonauthors.au_idlike’[1—5]%'whereauthors.city=publishers。city—-使用自联接查找居住在Oakland相同邮码区域中的作者。fromauthorsainnerjoinauthorsbwherea.city=’Oakland’anda.au_fname<〉b.au_fnameP所有题目selectascii('sql’)selectchar(66)selectcharindex(’E’,’HELLO’)-—结果:2selectleft('RICHARD',4)--结果:RICHselectlen(’RICHARD’)selectlower(’RICHARD')select'SQL'+ltrim('RICHARD')selectreverse(’ACTION')selectright('RICHARD',4)selectrtrim(’RICHARD')+’SQL'selectpatindex('%BOX%’,'ACTIONBOX')select’RICHARD’+space(2)+'HELL'——结果:RICHARDHELLselectstuff('Weather’,2,2,'I')selectsubstring(’Weather’,2,2)selectupper('Richard’)-—结果:RICHARDselectdateadd(dd,10,getdate())selectdatediff(dy,getdate(),’2005—01-01’)-—结果:-288selectdatepart(dw,’2004—10—01’)—-结果:6selectdatename(dw,’2004-10-01’)——结果:星期五——第七讲多表查询上机实验userecruitment外部候选人的信息select*fromExternalCandidatewheredatediff(yy,dbirthdate,getdate())between35and40—-—需要在当前日期之后的10天在报纸上登载一则广告,系统需要计算出日期并显示selectdistinctgetdate()astoday,dateadd(day,10,getdate())as’10daysfromtoday’fromnewsad-——统计外部候选人接受测试和面试日期的间隔的时间平均值selectavg(datediff(day,dtestdate,dinterviewdate))as测试面试日期间隔平均天数fromexternalcandidate——-需要获取外部候选人的姓名和他们申请的职位selectexternalcandidate。vcandidatenameas姓名,position.vdescriptionas申请职位fromexternalcandidateleftjoinpositiononexternalcandidate.cpositioncode=position.cpositioncode--—需要获得在2001年应聘的外部候选人的名字,及推荐他们的招聘机构名selectexternalcandidate.vcandidatenameas名字,ameas推荐他们的招聘机构名fromexternalcandidateleftjoinrecruitmentagenciesonexternalcandidate.cagencycode=recruitmentagencies.cagencycodewhereyear(externalcandidate.ddateofapplication)=2001---需要获取外部候选人的姓名以及他们的参照的招聘的广告所属的报纸名selectexternalcandidate.vcandidatenameas姓名,ewspapernameas参照招聘广告所属报纸fromexternalcandidate,newsad,newspaperwhereewsadno=ewspapercode=ewspapercode—-—需要获取大学名称、报纸名称以及它们地址的列表selectcollege.cCollegeNameas大学名称,college.vcollegeaddress学校地址,ewspapernameas报纸名称,newspaper。vhoaddressas报社地址fromcollege,newspaper—-问题:这两张表之间没有联系,那么应选用何种联接?否则这里面有太多冗余数据selectcollege.cCollegeNameas大学名称,college。vcollegeaddress学校地址,ewspapernameas报纸名称,newspaper.vhoaddressas报社地址fromcollege,newspaperwherecollege.ccity=newspaper。ccity--因为大学所在城市的值为某某,而报纸所在城市的值为某某市,因此按此不能正确查出结果--采用以下办法可以解决selectcollege.cCollegeNameas大学名称,college。vcollegeaddress学校地址,ewspapernameas报纸名称,newspaper.vhoaddressas报社地址fromcollege,newspaperwhereleft(ltrim(college。ccity),2)=left(ltrim(newspaper.ccity),2)—-还是显示出大学表里符合条件的记录与报纸表里符合条件的记录之积,内联接结果一样--第七讲多表查询作业useGlobalToyz-—按指定格式(详见学习手册P27)显示所有运货的报表(天数=实际到达日期-运货日期)selectcordernoas定单号,dshipmentdateas运货日期,dactualdeliverydateas实际到达日期,datediff(day,dshipmentdate,dactualdeliverydate)as运送天数fromshipment-—小结:两日期之差运算为第二个日期参数—第一个日期参数—-按指定格式(详见学习手册P27)显示所有的订单selectcOrderNoas定单号,cShopperIdas购物者号,dOrderDateas'订单日期(号)’,datename(dw,dorderdate)星期几fromorders—-小结:求星期几,日期元素只能用DW,而不能用WK,WK求得是在一年中的第几周,而列别名如果有--特殊字符需要引号引起来——显示所有玩具名和所属的种类名selecttoys.vToyNameas玩具名,Category.cCategoryas种类名fromcategoryjointoysontoys.cCategoryId=Category。cCategoryId——小结:交叉联接不能使用条件,而内联接和右外联接在此效果相同,-—左外联接和全外联接效果相同,但多出九条玩具名为空的记录,--因为左外联接时将显示所有左表中即种类表中的记录,即使没有该玩具属于该种类,—-JOIN前不加关键字时默认为内联接——用join联接表名时,后面条件语句只能先跟on关键字,不能直接用where——按指定格式(详见学习手册P27)显示所有玩具的名称、商标和种类selecttoys。vtoynameas玩具名,ToyBrand。cBrandNameas商标名,Category.ccategoryas类别名fromtoys,ToyBrand,Categorywheretoys。cBrandId=ToyBrand.cBrandIdandtoys.cCategoryId=Category.cCategoryId—-按指定格式(详见学习手册P28)显示所有玩具的订货号、玩具ID和玩具使用的礼品包装说明selectorderdetail.cordernoas定单号,orderdetail.ctoyidas玩具号,wrapper。vdescriptionas包装信息fromorderdetailleftjoinwrapperonorderdetail.cwrapperid=wrapper.cwrapperidselectorderdetail.cordernoas定单号,orderdetail.ctoyidas玩具号,wrapper.vdescriptionas包装信息fromtoys,orderdetail,wrapperwheretoys。ctoyid=orderdetail。ctoyidandorderdetail.cwrapperid=wrapper.cwrapperid——小结:外连接的关键字outer可以省略不写-—问题:采用以上方式查出的结果好象未能满足需求,没有显示所有的玩具,如果用三张表,即-—加入toys表后,加上一个toys.ctoyid=orderdetail.ctoyid后也不能列出所有玩具。—-按指定格式(详见学习手册P28)显示所有购物者名,及他们所购买的订单信息(无论购物者是否有订单)selectshopper.vfirstnameas购物者名,orders。cordernoas定单号,orders.dorderdateas定单时间,orders.mtotalcostas定单金额fromshopperleftjoinordersonshopper.cshopperid=orders。cshopperid——按指定格式(详见学习手册P28)显示订单号码、订单日期和每个订单所在的季节selectcOrderNoas定单号,dOrderDateas定单日期,datename(qq,dOrderDate)as季节fromorders句?——按指定格式(详见学习手册P28)显示所有购物者ID、名字、电话和相应订单的接受者selectshopper.cshopperidas购物者号,shopper。vfirstnameas名字,shopper。cphoneas电话,recipient。vfirstnameas接受者名,recipient。cphoneas电话fromshopper,orders,recipientwhereshopper。cshopperid=orders.cshopperidandorders。corderno=recipient.corderno—-小结:如果表与表之间联接没用JOIN,则条件语句关键字不能用ON,只能用WHERE—-按指定格式(详见学习手册P28)显示所有购物者和接受者的名字、地址selectshopper.vfirstnameas购物者名字,shopper.vaddressas购物者地址,recipient.vfirstnameas接受者名字,recipient.vaddressas接受者地址fromshopper,orders,recipientwhereshopper.cshopperid=orders。cshopperidandorders。corderno=recipient.corderno——显示所有玩具名及该玩具的销售数量selecttoys.vtoynameas玩具名,orderdetail.siqtyas销售数量fromtoysleftjoinorderdetailontoys.ctoyid=orderdetai

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论