欢迎光临
我们一直在努力

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

建站超值云服务器,限时71元/月

整理别人的sql


大概的思想是用union 和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
http://blog.csdn.net/kemy88/archive/2007/02/28/1516913.aspx

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » sql语句查询结果合并union all用法_数据库技巧
分享到: 更多 (0)