欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-13 12:12:58
复制代码 代码如下:--合并重复行 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

上一篇:

下一篇: