sql 查詢結果合并union all用法_數(shù)據(jù)庫技巧
更新時間:2009年11月08日 23:32:28 作者:
sql語句查詢結果合并union all用法_數(shù)據(jù)庫技巧,需要的朋友可以參考下。
復制代碼 代碼如下:
--合并重復行
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
相關文章
SQL Server 實現(xiàn)數(shù)字輔助表實例代碼
這篇文章主要介紹了SQL Server 實現(xiàn)數(shù)字輔助表的相關資料,并附實例代碼,需要的朋友可以參考下2016-10-10SQL 使用 VALUES 生成帶數(shù)據(jù)的臨時表實例代碼詳解
這篇文章主要介紹了SQL 使用 VALUES 生成帶數(shù)據(jù)的臨時表,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2007-09-09SQLServer Execpt和not in 性能區(qū)別
網(wǎng)上有很多 except 和 not in的返回結果區(qū)別這里就就提了2012-01-01SQLServer中JSON文檔型數(shù)據(jù)的查詢問題解決
SQL Server 對于數(shù)據(jù)平臺的開發(fā)者來說越來越友好,下面這篇文章主要給大家介紹了關于SQLServer中JSON文檔型數(shù)據(jù)的查詢問題的解決方法,需要的朋友可以參考下2021-06-06