当前位置: 移动技术网 > IT编程>数据库>MSSQL > sql 查询结果合并union all用法_数据库技巧

sql 查询结果合并union all用法_数据库技巧

2017年12月12日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:--合并重复行 select * from a union select * from b --不合并重复行 select * from a union
复制代码 代码如下:

--合并重复行
select * from a
union
select * from b


--不合并重复行
select * from a
union all
select * from b


按某个字段排序
--合并重复行
select *
from (
select * from a
union
select * from b) as t
order by 字段名

--不合并重复行
select *
from (
select * from a
union all
select * from b) as t
order by 字段名

//sql server版
select * from (
select top 2 id,adddate,title,url from barticle where classid=1 order by adddate desc) a
union all
select * from (
select top 2 id,adddate,title,url from barticle where classid=2 order by adddate desc) b
union all
select * from (
select top 2 id,adddate,title,url from barticle where classid=3 order by adddate desc) c
union all
select * from (
select top 2 id,adddate,title,url from barticle where classid=4 order by adddate desc) d


//mysql版
select * from (
select id,adddate,title,url from barticle where classid=1 order by adddate desc limit 0,2) a
union all
select * from (
select id,adddate,title,url from barticle where classid=2 order by adddate desc limit 0,2) b
union all
select * from (
select id,adddate,title,url from barticle where classid=3 order by adddate desc limit 0,2) c
union all
select * from (
select id,adddate,title,url from barticle where classid=4 order by adddate desc limit 0,2) d

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网