大概的思想是用union 和union all
--合并重复行
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语句查询结果合并union all用法_数据库技巧,需要的朋友可以参考下。
工作中常用的SQL + 超复杂SQL 交叉连接查询corss.txt 公交线路问题.txt 。。。。 充储过程--sql0.sql 分割字符串.txt 分布式查询.txt 分组排列进行更新.txt 别名.txt 动态SQL语句.txt ...unionall.txt
UNION指令的目的是将两个SQL语句的结果合并起来。从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料。
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
其中GRANT的常用用法如下: grant all on mydb.* to NewUserName@HostName identified by "password" ; grant usage on *.* to NewUserName@HostName identified by "password"; grant select,insert,update on mydb...
LINQ to SQL语句(8)之Concat/Union/Intersect/Except 32 Concat/Union/Intersect/Except操作 32 Concat(连接) 32 1.简单形式: 33 2.复 合形式: 33 Union(合并) 33 Intersect(相交) 34 Except(与非) 34 ...
6、合并查询结果 7、为表和字段取别名 8、使用正则表达式查询 什么是查询? 怎么查的? 数据的准备如下: [sql] view plain copy create table STUDENT( STU_ID int primary KEY, STU_NAME char(10) not null, ...
4.2.1 UNION和UNION ALL 103 4.2.2 MINUS 106 4.2.3 INTERSECT 107 4.3 集合与空值 108 4.3.1 空值与非直观结果 108 4.3.2 集合运算中的空值行为 110 4.3.3 空值与GROUP BY和ORDER BY 112 4.3.4 空值与聚合...
其中GRANT的常用用法如下: grant all on mydb.* to NewUserName@HostName identified by “password”; grant usage on *.* to NewUserName@HostName identified by “password”; grant select,insert,update ...
数据查询语言 (Data Query Language, DQL) 是SQL语言中,负责进行数据查询而不会对数据本身进行修改的语句,这是最基本的SQL语句。例如:SELECT(查询) 数据控制语言Data Controlling Language(DCL),用来...
14 <br>0028 “///”符号的使用技巧 14 <br>0029 使用注释取消程序语句的执行 15 <br>2.2 语句 15 <br>0030 跳转语句GOTO的使用 15 <br>0031 Continue语句的使用 16 <br>0032 Break...
32、SQL语句执行顺序 51 33、null的含义 52 34、mysql分页 52 35、MySQL、SqlServer、oracle写出字符存储、字符串转时间 52 36、update语句可以修改结果集中的数据吗? 53 37、oracle如何设置主键自动增长? 53 38、...