MySQL的高级查询
2016-03-14 22:11
615 查看
高级查询
1.连接查询(对列的扩展)
第一种形式
select * from Info,Nation #会形成笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code #加入筛选条件
select Info.Code,Info.Name,Sex,Nation.Name 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
#换表头
第二种形式:
select * from Info join Nation #join连接,形成笛卡尔积 查询很慢
select * from Info join Nation on Info.Nation = Nation.Code #join关键字
2.联合查询(对行的扩展)
select * from Info where Nation = 'n002'
union #关键字
select * from Info where Code = 'p002'
3.子查询(无关子查询) 如果子查询语句可以单独拿出来执行,就是无关查询
在一个sql语句中有两个子查询,其中一个a查询的结果作为另一个b的
select查询条件,a成为里层查询,b为外层查询或父查询
查询民族为'汉族'的人员信息:
select * from Info where Nation = ( select Code from Nation where Name = '汉族')
查询民族为 '汉族' 或者 '回族' 的人员信息
select * from Info where Nation in (select Code from Nation where Name = '汉族' or Name = '回族')
select * from Info where Nation not in (select Code from Nation where Name = '汉族' or Name = '回族')
#in 关键字 not in不在表里
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)
#b.Brand 是 查询条件,a.Brand 是 逐条的信息
1.连接查询(对列的扩展)
第一种形式
select * from Info,Nation #会形成笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code #加入筛选条件
select Info.Code,Info.Name,Sex,Nation.Name 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
#换表头
第二种形式:
select * from Info join Nation #join连接,形成笛卡尔积 查询很慢
select * from Info join Nation on Info.Nation = Nation.Code #join关键字
2.联合查询(对行的扩展)
select * from Info where Nation = 'n002'
union #关键字
select * from Info where Code = 'p002'
3.子查询(无关子查询) 如果子查询语句可以单独拿出来执行,就是无关查询
在一个sql语句中有两个子查询,其中一个a查询的结果作为另一个b的
select查询条件,a成为里层查询,b为外层查询或父查询
查询民族为'汉族'的人员信息:
select * from Info where Nation = ( select Code from Nation where Name = '汉族')
查询民族为 '汉族' 或者 '回族' 的人员信息
select * from Info where Nation in (select Code from Nation where Name = '汉族' or Name = '回族')
select * from Info where Nation not in (select Code from Nation where Name = '汉族' or Name = '回族')
#in 关键字 not in不在表里
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)
#b.Brand 是 查询条件,a.Brand 是 逐条的信息
相关文章推荐
- mysql安装zip
- 深入分析MySQL ERROR 1045 (28000)
- 图解MySQL5.5详细安装与配置过程
- [mysql] mysql 5.6.X 慢查询日志
- mysql主从配置完整版(二进制安装)
- mysql安装常见问题(系统找不到指定的文件、发生系统错误 1067 进程意外终止)
- mysql-5.7.10-winx64 MySQL服务无法启动,服务没有报告任何错误的解决办法
- MySQL用户管理
- Mysql优化(三)sql语句优化
- SQL语句执行顺序(MySQL)
- MySQL基本命令 (二)
- MYSQL查询优化
- MySQL安装配置教程
- mysql插入返回当前生成的主键
- MySQL闪退问题的解决
- MySql 唯一键
- MySQL命令介绍
- MySQL学习笔记7(查询数据)
- 构建Mysql连接池
- mysql 数据库设置中文输入