sql 查詢成果歸並union all用法_數據庫技能。本站提示廣大學習愛好者:(sql 查詢成果歸並union all用法_數據庫技能)文章只能為提供參考,不一定能成為您想要的結果。以下是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