首页 > 资讯列表 > 编程/数据库 >> 数据库操作教程

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

数据库操作教程 2022-09-23 16:54:35 转载来源: 网络整理/侵权必删

复制代码代码如下:--合并重复行select*fromAunionselect*fromB--不合并重复行select*fromAunionallselect*fromB按某个字段排序--合并重复行select*from(select*fromAunionselect*fromB)ASTorderby字段名--不合并重复行select*from(select*fromAunionallselect*fromB)ASTorderby字段名//sqlserver版Select*From(selecttop2id,adddate

复制代码 代码如下:

--合并重复行
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 查询 结果 合并 unionall 用法 数据库 技巧


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持