Skip to content
久久日记本
曾经年少爱追梦,一心只想往前飞
  • 首页
  • 博客
    • 博客历史
    • 主题
    • 个人文集
  • 关于
    • 正在读的书
    • 作品归档
    • 2018作品归档
    • 联系我
  • 友情链接
  • 留言板
❄
❅
❆
❄
❅
❆
❄
❅
❆
❄
Code/DB

SQL基础复习(2)

Posted on 2012年8月7日 by 九九 / 929 Views

1、各种连接查询结果示意图

screenshot

2、关联查询

select * from Person,Class
where Person.ClassID = Class.ID

select * from Person p,Class c
where p.ClassID = c.ID

select *, 2012-year(p.birthday) age from Person p,Class c
where p.ClassID = c.ID

--内连接查询
select * from person
inner join class on person.classid = class.id
inner join xxxx on xxxx.xxx = person.xxxx
where sex = '男'

--左连接查询
select * from person
left join class on person.classid = class.id

--右连接查询
select * from person
right join class on person.classid = class.id

--全连接查询
select * from person
full join class on person.classid = class.id


--将查询结果作为另一个查询的条件使用
--查询结果用作查询条件进行比较运算的时候,结果必须是一行一列的
select * from person where salary = (select max(salary) from person )

--查询结果用作查询条件进行in查询的时候,结果必须是多行一列的
select * from person where classID in (select id from Class )


--可以将一个查询结果看成一个临时表,再次对它进行查询
select * from Person
inner join
(
 select classid,avg(salary) avgsalary
 from person
 group by classid
) Personavg on person.classid = personavg.classid
where person.salary < personavg.avgsalary


select * from Person,
(
 select classid,avg(salary) avgsalary
 from person
 group by classid
) Personavg
where person.classid = personavg.classid
and person.salary < personavg.avgsalary

3、其他查询

--any表示取查询结果的任意一条记录
select * from person
where classid = 2 and salary > any(select salary from person where classid=1)

--all表示取查询结果的所有值
select * from person
where classid = 2 and salary > all(select salary from person where classid=1)

--exists表示数据存在
select * from person
where exists(select * from class where 1<>1)

select * from person p
where exists(select * from class c where p.classid = c.id)

--union合并两个数据集,并且消除重复行
select * from person
union
select * from person1

select name,sex,salary from person
union
select name,sex,salary from person1
MSSQL, 网新实训笔记
九九
过去的我们,现在的自己,往事,终会随风而逝。 View all posts by 九九 →

Post navigation

Older post
SQL基础复习(1)
Newer post
SQL基础复习(3)-T-SQL

标签云

Android ASP.NET Baby C# C/C++ CSS Div DX11 flask front-end GAE Git Java JJProject JS Life MSSQL MVC OpenSource Oracle Python React React-Native Software Tools Vue Webpack Website Window WP7 乱记 十年旧梦 天气 宝宝成长日记 小说 工作 情感 故障 散文 日记 游戏开发 网新实训笔记 花落梧桐 诗间集 转载

时光机

  • 2023年2月
  • 2022年12月
  • 2022年4月
  • 2022年3月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年5月
  • 2019年12月
  • 2019年10月
  • 2019年9月
  • 2019年6月
  • 2019年5月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年3月
  • 2018年2月
  • 2018年1月
  • 2017年11月
  • 2017年10月
  • 2017年9月
  • 2017年7月
  • 2017年3月
  • 2017年1月
  • 2016年12月
  • 2016年11月
  • 2016年10月
  • 2016年7月
  • 2016年3月
  • 2016年2月
  • 2016年1月
  • 2015年12月
  • 2015年11月
  • 2015年10月
  • 2015年9月
  • 2015年8月
  • 2015年7月
  • 2015年4月
  • 2015年3月
  • 2015年2月
  • 2015年1月
  • 2014年12月
  • 2014年11月
  • 2014年10月
  • 2014年9月
  • 2014年8月
  • 2014年7月
  • 2014年6月
  • 2014年5月
  • 2014年4月
  • 2014年3月
  • 2014年2月
  • 2014年1月
  • 2013年12月
  • 2013年11月
  • 2013年10月
  • 2013年9月
  • 2013年8月
  • 2013年7月
  • 2013年6月
  • 2013年5月
  • 2013年4月
  • 2013年3月
  • 2013年1月
  • 2012年11月
  • 2012年10月
  • 2012年9月
  • 2012年8月
  • 2012年7月
  • 2012年6月
  • 2012年5月
  • 2012年4月
  • 2012年3月
  • 2012年2月
  • 2012年1月
  • 2011年12月
  • 2011年11月
  • 2011年10月
  • 2011年9月
  • 2011年8月
  • 2011年6月
  • 2011年5月
  • 2011年4月
  • 2011年3月
  • 2011年2月
  • 2010年12月
  • 2010年11月
  • 2010年10月
  • 2010年9月
  • 2010年8月
  • 2010年6月
  • 2010年5月
  • 2010年2月
  • 2010年1月
  • 2009年12月
  • 2009年11月
  • 2009年10月
  • 2009年9月
  • 2009年8月
  • 2009年7月
  • 2009年6月
  • 2009年5月
  • 2009年4月
  • 2009年3月
  • 2009年2月
  • 2009年1月
  • 2008年8月
  • 2008年6月
  • 2008年5月
  • 2008年4月
  • 2008年2月
  • 2007年11月
  • 2007年8月
  • 2007年6月
  • 2007年5月
  • 2007年4月
  • 2007年3月
  • 2007年2月
  • 2007年1月
  • 2006年10月
  • 2006年8月
© 2006 - 2023 久久日记本
Powered by WordPress | Theme: Graphy for 99diary