2016/3/16 高级查询 ①连接查询 ②联合查询 ③子查询 无关 相关
2016-03-16 21:15
363 查看
高级查询
1.连接查询(对列的扩展)
第一种形式: 注意 , where 和后面 join on的比较 join换 ,号 on换where
select * from Info,Nation #会形成笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code #加上筛选条件
select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where
Info.Nation = Nation.Code #查询指定列
select Info.Code as '代号',Info.Name as '姓名',Sex as '性别',Nation.Name as '民
族',Birthday as '生日' from Info,Nation where Info.Nation = Nation.Code #换表头
第二种形式: 注意 , where 和后面 join on的比较 join换 ,号 on换where
select * from Info join Nation #join连接
select * from Info join Nation on Info.Nation = Nation.Code #join on关键字
2.联合查询(对行的扩展) 关键词 union
select * from Info where Nation = 'n002'
union
select * from Info where Code = 'p002'
3.子查询(无关子查询)
在一个SQL语句中,至少有两个查询,其中一个a查询的结果作为另一个b的查询条件,a成为里层
查询或者子查询,
b成为外层查询或父查询。
查询民族为“汉族”的人员信息: 关键符号 =
select * from Info where Nation =(select Code from Nation where Name = '汉族')
查询民族为“汉族”或者"回族"的人员信息 关键词 in
select * from Info where Nation in (select Code from Nation where Name = '汉族' or
Name = '回族')
4.子查询(相关子查询)
查询同一系列的 油耗要比平均油耗低的汽车信息
子查询:select avg(Oil) from Car where Brand = ''
父查询:select * from Car where Oil< 平均油耗
select * from Car a where a.Oil <(select avg(b.Oil) from Car b where b.Brand =
a.Brand)
1.连接查询(对列的扩展)
第一种形式: 注意 , where 和后面 join on的比较 join换 ,号 on换where
select * from Info,Nation #会形成笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code #加上筛选条件
select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where
Info.Nation = Nation.Code #查询指定列
select Info.Code as '代号',Info.Name as '姓名',Sex as '性别',Nation.Name as '民
族',Birthday as '生日' from Info,Nation where Info.Nation = Nation.Code #换表头
第二种形式: 注意 , where 和后面 join on的比较 join换 ,号 on换where
select * from Info join Nation #join连接
select * from Info join Nation on Info.Nation = Nation.Code #join on关键字
2.联合查询(对行的扩展) 关键词 union
select * from Info where Nation = 'n002'
union
select * from Info where Code = 'p002'
3.子查询(无关子查询)
在一个SQL语句中,至少有两个查询,其中一个a查询的结果作为另一个b的查询条件,a成为里层
查询或者子查询,
b成为外层查询或父查询。
查询民族为“汉族”的人员信息: 关键符号 =
select * from Info where Nation =(select Code from Nation where Name = '汉族')
查询民族为“汉族”或者"回族"的人员信息 关键词 in
select * from Info where Nation in (select Code from Nation where Name = '汉族' or
Name = '回族')
4.子查询(相关子查询)
查询同一系列的 油耗要比平均油耗低的汽车信息
子查询:select avg(Oil) from Car where Brand = ''
父查询:select * from Car where Oil< 平均油耗
select * from Car a where a.Oil <(select avg(b.Oil) from Car b where b.Brand =
a.Brand)
相关文章推荐
- [code]最长回文子串
- Java单例模式深入详解
- 排序算法小结
- Activity启动模式
- Codeforces Round #334 (Div. 2) B C D
- [DP 上下界最小流] BZOJ4200 [Noi2015]小园丁与老司机
- Android中常用的存储方式
- Android中ListView的几种常见的优化方法
- misc_register杂项设备
- 两个Hacker,专门Patch TObject
- js事件防止冒泡
- 并查集
- 蓝桥杯 算法训练 大小写转换
- Wu-Manber字符串多模式匹配
- CodeForces 148C Terse princess(模拟)
- IM同步服务
- 利用Wireshark分析TCP三次握手
- 算法基础:递归---Fibonacci数列
- poj3928 Ping pong 树状数组
- iOS如何获取网络图片(二)