sql 多表查询 代码示例.doc

上传人:飞****9 文档编号:136127958 上传时间:2020-06-24 格式:DOC 页数:5 大小:24KB
返回 下载 相关 举报
sql 多表查询 代码示例.doc_第1页
第1页 / 共5页
sql 多表查询 代码示例.doc_第2页
第2页 / 共5页
sql 多表查询 代码示例.doc_第3页
第3页 / 共5页
sql 多表查询 代码示例.doc_第4页
第4页 / 共5页
sql 多表查询 代码示例.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《sql 多表查询 代码示例.doc》由会员分享,可在线阅读,更多相关《sql 多表查询 代码示例.doc(5页珍藏版)》请在金锄头文库上搜索。

1、SQL多表查询代码示例在Pubs数据库中,完成以下查询use pubs-使用内联接查询出authors和publishers表中位于同一个城市的作者和出版社信息select au_fname+au_lname as 作者,pub_name as 出版社from authors inner join publisherson authors.city=publishers.city-查询出作者号以15开头的所有作者,并使用右外联接在查询的结果集中列出和作者-在同一个城市的出版社名select au_fname+au_lname as 作者,pub_name as 出版社from authors

2、right outer join publisherson authors.au_id like 1-5% where authors.city=publishers.city-使用自联接查找居住在 Oakland 相同邮码区域中的作者。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=Oakland and a.au_fname b.au_fname-P26学习手册上机试验的所有题目select asc

3、ii(sql)-结果:115select char(66)-结果:Bselect charindex(E,HELLO)-结果:2select left(RICHARD,4)-结果:RICHselect len(RICHARD)-结果:7select lower(RICHARD)-结果:richardselect SQL+ltrim(RICHARD)-结果:SQLRICHARDselect reverse(ACTION)-结果:NOITCAselect right(RICHARD,4)-结果:HARDselect rtrim(RICHARD )+SQL-结果:RICHARDSQLselect p

4、atindex(%BOX%,ACTIONBOX)-结果:7select RICHARD+space(2)+HELL-结果:RICHARD HELLselect stuff(Weather,2,2,I)-结果:WItherselect substring(Weather,2,2)-结果:easelect upper(Richard)-结果:RICHARDselect dateadd(dd,10,getdate()-结果:2005-10-26 16:04:58.030select datediff(dy,getdate(),2005-01-01)-结果:-288select datepart(dw

5、,2004-10-01)-结果:6select datename(dw,2004-10-01)-结果:星期五-第七讲 多表查询上机实验use recruitment- 需要得到年龄在35岁到40岁之间的外部候选人的信息select * from ExternalCandidatewhere datediff(yy,dbirthdate,getdate() between 35 and 40- 需要在当前日期之后的10天在报纸上登载一则广告,系统需要计算出日期并显示select distinct getdate() as today,dateadd(day,10,getdate() as 10

6、days from todayfrom newsad- 统计外部候选人接受测试和面试日期的间隔的时间平均值select avg(datediff(day,dtestdate,dinterviewdate) as 测试面试日期间隔平均天数from externalcandidate- 需要获取外部候选人的姓名和他们申请的职位select externalcandidate.vcandidatename as 姓名, position.vdescription as 申请职位from externalcandidate left join positionon externalcandidate.

7、cpositioncode=position.cpositioncode- 需要获得在2001年应聘的外部候选人的名字,及推荐他们的招聘机构名select externalcandidate.vcandidatename as 名字, ame as 推荐他们的招聘机构名from externalcandidate left join recruitmentagencieson externalcandidate.cagencycode=recruitmentagencies.cagencycodewhere year(externalcandidate.ddateofapplication)=

8、2001- 需要获取外部候选人的姓名以及他们的参照的招聘的广告所属的报纸名select externalcandidate.vcandidatename as 姓名, ewspapername as 参照招聘广告所属报纸from externalcandidate,newsad,newspaperwhere ewsadno=ewsadno and ewspapercode=ewspapercode- 需要获取大学名称、报纸名称以及它们地址的列表select college.cCollegeName as 大学名称,college.vcollegeaddress 学校地址, ewspaperna

9、me as 报纸名称,newspaper.vhoaddress as 报社地址from college,newspaper-问题:这两张表之间没有联系,那么应选用何种联接?否则这里面有太多冗余数据- 是否为同一所城市里有哪些大学和哪些报纸?select college.cCollegeName as 大学名称,college.vcollegeaddress 学校地址, ewspapername as 报纸名称,newspaper.vhoaddress as 报社地址from college,newspaperwhere college.ccity=newspaper.ccity-因为大学所在城

10、市的值为某某,而报纸所在城市的值为某某市,因此按此不能正确查出结果-采用以下办法可以解决select college.cCollegeName as 大学名称,college.vcollegeaddress 学校地址, ewspapername as 报纸名称,newspaper.vhoaddress as 报社地址from college,newspaperwhere left(ltrim(college.ccity),2)=left(ltrim(newspaper.ccity),2)-还是显示出大学表里符合条件的记录与报纸表里符合条件的记录之积,内联接结果一样-第七讲 多表查询作业-P26

11、学习手册上机作业的所有题目use GlobalToyz-按指定格式(详见学习手册P27)显示所有运货的报表(天数=实际到达日期-运货日期)select corderno as 定单号, dshipmentdate as 运货日期, dactualdeliverydate as 实际到达日期, datediff(day,dshipmentdate,dactualdeliverydate) as 运送天数from shipment-小结:两日期之差运算为第二个日期参数-第一个日期参数-按指定格式(详见学习手册P27)显示所有的订单select cOrderNo as 定单号,cShopperId

12、as 购物者号,dOrderDate as 订单日期(号), datename(dw,dorderdate)星期几from orders-小结:求星期几,日期元素只能用DW,而不能用WK,WK求得是在一年中的第几周,而列别名如果有- 特殊字符需要引号引起来-显示所有玩具名和所属的种类名select toys.vToyName as 玩具名,Category.cCategory as 种类名from category join toyson toys.cCategoryId = Category.cCategoryId-小结:交叉联接不能使用条件,而内联接和右外联接在此效果相同,- 左外联接和全

13、外联接效果相同,但多出九条玩具名为空的记录,- 因为左外联接时将显示所有左表中即种类表中的记录,即使没有该玩具属于该种类,- 此时玩具名为NULL值- JOIN前不加关键字时默认为内联接- 用join联接表名时,后面条件语句只能先跟on关键字,不能直接用where-按指定格式(详见学习手册P27)显示所有玩具的名称、商标和种类select toys.vtoyname as 玩具名,ToyBrand.cBrandName as 商标名, Category.ccategory as 类别名from toys,ToyBrand,Categorywhere toys.cBrandId=ToyBrand

14、.cBrandId and toys.cCategoryId=Category.cCategoryId-问题:如果用逗号联系多张表,之间采用的是什么联接方式?表与表之间的前后顺序影不影响结果?-按指定格式(详见学习手册P28)显示所有玩具的订货号、玩具ID和玩具使用的礼品包装说明select orderdetail.corderno as 定单号,orderdetail.ctoyid as 玩具号, wrapper.vdescription as 包装信息from orderdetail left join wrapperon orderdetail.cwrapperid=wrapper.cwrapperidselect orderdetail.corderno as 定单号,orderdetail.ctoyid as 玩具号, wrapper.vdescription as 包装信息from toys,orderdetail,wrapperwhere toys.ctoy

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号