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

 我来答
竺萌鹿德
2019-11-13 · TA获得超过3623个赞
知道大有可为答主
回答量:3126
采纳率:25%
帮助的人:178万
展开全部
复制代码
代码如下:
--合并重复行
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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式