博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ylb: SQL表的高级查询-子查询
阅读量:4683 次
发布时间:2019-06-09

本文共 4459 字,大约阅读时间需要 14 分钟。

ylbtech-SQL Server: SQL Server- SQL表的高级查询-子查询

 SQL Server 表的高级查询-子查询。

1,ylb:表的高级查询-子查询
--================================-- ylb:表的高级查询-子查询--    pubs库的练习-- 12/12/2011--================================use pubsgoselect * from authorsselect * from titleauthorselect * from titlesselect * from publishersselect * from storesgo--1. 查找和出版商同一州的作者姓名。select * from authors awhere state in(select state from publishers where state=a.state)goselect * from authors awhere exists(select * from publishers where state=a.state)go--2. 查找和商店同一州的作者姓名select * from authors awhere state in(select state from stores where state=a.state)go--3. 查找和商店同一城市的出版社名称select * from publishers pwhere city in (select city from stores where city=p.city)go--4. 查找写商业书的作者名select * from authorsselect * from titleauthorselect * from titlesgo--4_1,select title_id from titleswhere type='business'go--4_2,select au_id from titleauthorwhere title_id in('BU1032','BU1111','BU2075','BU7832')go--4_3,select * from authorswhere au_id in('213-46-8915','267-41-2394')go--4,结论select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from titleswhere type='business'))go--5. 查找美国出版社出版的所有书select * from publishersselect * from titlesgo--5_1,select pub_id from publisherswhere country='USA'go--5_2,select * from titleswhere pub_id in('0877','0736')go--5结论select * from titleswhere pub_id in(select pub_id from publisherswhere country='USA')go--6. 查找美国出版社出版书的作者姓名--6_1,select pub_id from publisherswhere country='USA'go--6_2,select title_id from titleswhere pub_id in('0877','0736')go--6_3,select au_id from titleauthorwhere title_id in('BU2075','MC2222')go--6_4,select * from authorswhere au_id in('213-46-8915','712-45-1867')go--6总结select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from titleswhere pub_id in(select pub_id from publisherswhere country='USA')))go--7. 查找在CA州出版社所出版的商业书作者姓名--7-1,select pub_id from publisherswhere state='CA'go--7-2,select title_id from  titleswhere pub_id in('1389')and [type]='business'go--7-3,select au_id from titleauthorwhere title_id in('BU1032','BU1111')go--7-4,select * from authorswhere au_id in('213-46-8915','409-56-7008')go--7总结select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from  titleswhere pub_id in(select pub_id from publisherswhere state='CA')and [type]='business'))go--P:8. 查找和出版社在同一州的作者所写的书名--8_1,select au_id from authors awhere state in(select state from publishers where state=a.state)go--8-2,select title_id from titleauthorwhere au_id in(select au_id from authors awhere state in(select state from publishers where state=a.state))go--8-3,select * from titleswhere title_id in(select title_id from titleauthor)go--8 结论select * from titleswhere title_id in(select title_id from titleauthorwhere au_id in(select au_id from authors awhere state in(select state from publishers where state=a.state)))go--9. 查找和作者在同一城市的出版社名称select * from publishers pwhere city in(select city from authors where city=p.city)go--10. 查找单价大于所有商业书的书,它的作者姓名--方法一、--10-1,select MAX(price) from titles where type='business'go--10-2a,select title_id from titleswhere price >(select MAX(price) from titles where type='business')go--10-2b,select title_id from titleswhere price > all(select price from titles where type='business')go--10-3,select au_id from titleauthorwhere title_id in(select title_id from titleswhere price >(select MAX(price) from titles where type='business'))go--10总结select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from titleswhere price >(select MAX(price) from titles where type='business')))go--11.   查找(Algodata Infosystems)出版社所在州,出过商业书的作者姓名--11_1,select pub_id from publisherswhere pub_name='Algodata Infosystems'go--11-2,select title_id from titleswhere type='business' and pub_id =(select pub_id from publisherswhere pub_name='Algodata Infosystems')go--11-3,select au_id from titleauthorwhere title_id in(select title_id from titleswhere pub_id =(select pub_id from publisherswhere pub_name='Algodata Infosystems'))go--11-4,select * from authorswhere au_id in(select au_id from titleauthorwhere title_id in(select title_id from titleswhere type='business' and pub_id =(select pub_id from publisherswhere pub_name='Algodata Infosystems')))
warn 作者:
出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/ylbtech/p/3498205.html

你可能感兴趣的文章
项目开发总结报告(GB8567——88)
查看>>
enumerate使用
查看>>
BZOJ1930: [Shoi2003]pacman 吃豆豆
查看>>
SSH加固
查看>>
端口扫描base
查看>>
iOS IM开发的一些开源、框架和教程等资料
查看>>
FansUnion:共同写博客计划终究还是“流产”了
查看>>
python 二维字典
查看>>
编译原理实验一
查看>>
Git for Android Studio 学习笔记
查看>>
pip 警告!The default format will switch to columns in the future
查看>>
Arrays类学习笔记
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
闲的折腾——自己动手更换油雾分离阀/废气阀
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
发布时间 sql语句
查看>>
黑马程序员 ExecuteReader执行查询
查看>>