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

复制代码 代码如下:

–合并重复行

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

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐