程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> sql語句查詢結果合並union all用法_數據庫技巧

sql語句查詢結果合並union all用法_數據庫技巧

編輯:關於SqlServer

vIEw plaincopy to clipboardprint?
--合並重復行  
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)
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved