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