联合结果集union (集合运算符)

-------------------------使用union联合结果集----------------
select tsname,tsgender,tsage from TblStudent
union all
select fname,fgender,fage from MyStudent
-------------------------------------------- --使用union和union all都能进行联合,区别在于:使用union联合去除重复、重新排列数据,而union all不会去除重复也不会重新排列。
select select tsname,tsgender,tsage from TblStudent
union
select fname,fgender,fage from MyStudent
--大多数情况下,联合的时候不需要去除重复,同时要保持数据的顺序,所以一般建议使用union all

--从MyOrder表中统计每种商品的销售总价,并且在底部做汇总

select
商品名称,
销售总价=sum(销售价格*销售数量)
from MyOrder
group by 商品名称
union all
select
'总销售价格',
sum(销售价格*销售数量)
order by 销售总价 asc --要查询成绩表中的:最高分,最低分,平均分 select
max(tmath) as 最高分,
min(tmath) as 最低分,
avg(tmath) as 平均分
from MyOrder select 名称='最高分',分数=max(tmath) from TblScore
union all
select 名称='最低分',分数=min(tmath) from TblScore
union all
select 名称='平均分',分数=avg(tmath) from TblScore
-------------------------使用union向表中插入多条数据-----------------------------
select * from TblStudent
insert into TblStudent
select '赵玉西','男','北京市海定区','',18,'1990-9-9','',3
union all
select '赵玉西1','男','北京市海定区1','',18,'1990-9-6','',1
union all
select '赵玉西2','男','北京市海定区2','',18,'1990-9-3','',5
union all
select '赵玉西3','男','北京市海定区3','',18,'1990-9-2','',4
union all
select '赵玉西4','男','北京市海定区4','',18,'1990-9-1','',6 ---在使用union进行插入数据的时候,也要注意union会去除重复的
select * from TblStudent
insert into TblStudent
select '赵玉西','男','北京市海定区','',18,'1990-9-9','',3
union
select '赵玉西','男','北京市海定区','',18,'1990-9-9','',3
union
select '赵玉西','男','北京市海定区','',18,'1990-9-9','',3
union
select '赵玉西','男','北京市海定区','',18,'1990-9-9','',3

最好还是使用union all。

最新文章

  1. Switch重构处理
  2. String()与 toString()
  3. 关于mysql数据库字符集优先级问题
  4. 理解 Linux 网络栈(1):Linux 网络协议栈简单总结
  5. 【原】storm源码之storm代码结构【译】
  6. 第三方登录(1)OAuth(开放授权)简介及授权过程
  7. Spring AOP--基于XML文件的配置
  8. ASP.NET Application_Error错误日志写入
  9. hdu 3395
  10. 惠普4431s 笔记本配置
  11. jconsole 连接 eclipse启动项
  12. docker install for centos7
  13. 网上搜集的一段php可逆加密函数
  14. Linux下一次删除百万文件
  15. 使用Myeclipse为数据表创建hibernate实体对象
  16. 《清华梦的粉碎》by王垠
  17. 了解Vue.js
  18. (04) springboot 下的springMVC和jsp和mybatis
  19. Day05 (黑客成长日记) 文件操作系列
  20. JVM jinfo命令(Java Configuration Info) 用法小结

热门文章

  1. 通过transmittable-thread-local源码理解线程池线程本地变量传递的原理
  2. Spark 源码系列(六)Shuffle 的过程解析
  3. 面试题总结-Java部分
  4. jQuery学习(二)
  5. LNMP企业应用部署全过程(基于DEDE后台)
  6. 最小生成树之prime算法
  7. 播放音乐(mciSendString)
  8. Oracle触发器之系统触发器
  9. 王颖奇 20171010129《面向对象程序设计(java)》第十周学习总结
  10. Mybatis 分页:Pagehelper + 拦截器实现