7.union
2016-03-04 11:58
148 查看
联合结果集union
简单的结果集联合:
select number,name,age from emp
union
select cardnumber,name,age from emp2
基本的原则:每个结果集必须有相同的列数,每个结果集的列必须类型相容。
select number,name,age,dept from emp
union
select cardnumber,name,age,'temp' from emp2
union因为要进行重复值扫描,所以效率低,因此如果不是确定要合并重复行,那么就用union all。
案例1:
要求查询员工的最低年龄和最高年龄,临时工和正式员工要分别查询
select '正式员工最高年龄', max(age) from emp
union all
select '正式员工最高年龄', min(age) from emp
union all
select '临时员工最高年龄', max(age) from emp
union all
select '临时员工最高年龄', min(age) from emp
案例二
查询每位正式员工的信息,包括工号,工资,并且在最后一行加上所有员工工资额合计
select number,salary from emp
union
select '工资合计',sum(salary) from emp
简单的结果集联合:
select number,name,age from emp
union
select cardnumber,name,age from emp2
基本的原则:每个结果集必须有相同的列数,每个结果集的列必须类型相容。
select number,name,age,dept from emp
union
select cardnumber,name,age,'temp' from emp2
union因为要进行重复值扫描,所以效率低,因此如果不是确定要合并重复行,那么就用union all。
案例1:
要求查询员工的最低年龄和最高年龄,临时工和正式员工要分别查询
select '正式员工最高年龄', max(age) from emp
union all
select '正式员工最高年龄', min(age) from emp
union all
select '临时员工最高年龄', max(age) from emp
union all
select '临时员工最高年龄', min(age) from emp
案例二
查询每位正式员工的信息,包括工号,工资,并且在最后一行加上所有员工工资额合计
select number,salary from emp
union
select '工资合计',sum(salary) from emp
相关文章推荐
- 8.数据库函数
- mac 下 使用 java运行 class 文件 总是提示 “错误: 找不到或无法加载主类”的解决方法
- 欺诈网站都注重用户体验!你,还在等什么?!
- java缓存技术的介绍
- 基于PHP实现等比压缩图片大小
- NIO的java.nio.channels.CancelledKeyException几种解决方法
- Reverse words in a string by java
- (1)单例模式
- PHP学习手册 字符串(一)
- 1.数据库概念
- 找出程序中的bug——调试
- MySQL正则表达式 REGEXP详解
- PHP文件夹管理类
- 线性筛法 欧拉函数
- 线性筛法 欧拉函数
- HDU 1827 Summer Holiday(Tarjan缩点)
- java-IO
- MFC总结的小知识点
- 毕业3年半,我是如何靠自己挣钱在北京买房的
- iOS学习资料