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

前端之家收集整理的这篇文章主要介绍了sql 查询结果合并union all用法_数据库技巧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="36812" class="copybut" id="copybut36812" onclick="doCopy('code36812')"> 代码如下:

<div class="codebody" id="code36812">
--合并重复行
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,url from bArticle where ClassId=2 order by adddate desc) B
Union All
Select From (
select top 2 id,url from bArticle where ClassId=3 order by adddate desc) C
Union All
Select
From (
select top 2 id,url from bArticle where ClassId=4 order by adddate desc) D
//MysqL
Select From (
select id,url from bArticle where ClassId=1 order by adddate desc limit 0,2) A
Union All
Select
From (
select id,url from bArticle where ClassId=2 order by adddate desc limit 0,2) B
Union All
Select From (
select id,url from bArticle where ClassId=3 order by adddate desc limit 0,2) C
Union All
Select
From (
select id,url from bArticle where ClassId=4 order by adddate desc limit 0,2) D

猜你在找的MsSQL相关文章