mysql高级查询
2016-04-23 19:43
417 查看
高级查询:
1.连接查询
select * from Info,Nation #得出的结果称为笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code
join on连接
select * from Info join Nation #join连接
select * from Info join Nation on Info.Nation = Nation.Code
2.联合查询
select Code,Name from Info
union
select Code,Name from Nation
3.子查询
1)无关子查询
select Code from Nation where Name = '汉族' #去Nation表中查询汉族的民族代号
select * from Info where Nation = (民族代号) #在Info表中查询民族代号为上一个查询结果的所有信息
select * from Info where Nation = (select Code from Nation where Name = '汉族')
子查询查询的结果被父查询使用,子查询可以单独执行的成为无关子查询
2)相关子查询
select * from Car where Oil<(该系列的平均油耗) #查询油耗小于该系列平均油耗的
select avg(Oil) from Car where Brand = "值" #查询某系列的平均油耗
select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)
1.连接查询
select * from Info,Nation #得出的结果称为笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code
join on连接
select * from Info join Nation #join连接
select * from Info join Nation on Info.Nation = Nation.Code
2.联合查询
select Code,Name from Info
union
select Code,Name from Nation
3.子查询
1)无关子查询
select Code from Nation where Name = '汉族' #去Nation表中查询汉族的民族代号
select * from Info where Nation = (民族代号) #在Info表中查询民族代号为上一个查询结果的所有信息
select * from Info where Nation = (select Code from Nation where Name = '汉族')
子查询查询的结果被父查询使用,子查询可以单独执行的成为无关子查询
2)相关子查询
select * from Car where Oil<(该系列的平均油耗) #查询油耗小于该系列平均油耗的
select avg(Oil) from Car where Brand = "值" #查询某系列的平均油耗
select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)
相关文章推荐
- mysql 查看优化器重写后的sql
- mysql提示:host xxxx not allowe to connect
- 取消MySQL timestamp列默认ON UPDATE CURRENT_TIMESTAMP
- MySQL中的datetime与timestamp区别比较,以及选择方法
- mysql主从复制汇总
- 数据库 mysql 语句
- mysql 使用中文
- mysql存储过程详解
- MySql的隔离级别和锁的关系
- MySQL 数据库 练习题
- MySQL 数据库 常用函数
- mysql group_cocnat()最大限制
- 如何记录MySQL执行过的SQL语句?
- MySQL 入门教程
- mysql 数据库服务中的应用程序
- MySQL 数据库 高级查询
- 【服务器编程】MYSQL数据库连接池封装
- 【mysql 】sql删除重复记录 You can't specify target table '表名' for update in FROM clause
- mysql表导出导入测试(utf8-utf8)
- 测试mysql单表排序是否有走索引