整理别人的sql

大概的思想是用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用法的更多相关文章

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

    --合并重复行 select * from A union select * from B --不合并重复行 select * from A union all select * from B 按某个 ...

  2. mysql sql语句多表合并UNION ALL和UNION

    select d1.ID,CAST(d1.ID AS CHAR) AS intId, d1.CODE_TYPE, d1.CODE, d1.CODE_IMG, d1.VALUE from m_dict_ ...

  3. 使用sql语句查询日期在一定时间内的数据

    使用sql语句查询日期在一周内的数据 select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0   //查询当天日期在一周年 ...

  4. 浅谈MySQL中优化sql语句查询常用的30种方法 - 转载

    浅谈MySQL中优化sql语句查询常用的30种方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使 ...

  5. sql语句查询

    1. sql语句查询某位数字或者某几位数字开头的数据,字段类型为数字类: %’: 2. sql搜索以4开头和含有李字的数据: select * from wlzbpre_user where real ...

  6. phpcmsv9自定义sql语句查询模型实现

    在phpcmsv9中,自定义sql语句查询可不太好实现,传入sql语句查询很容易被内部转入生成一系列莫名其妙的sql语句,比如最佳前缀等等,直接造成sql语句查询错误,在此也提供两种解决办法,1修改底 ...

  7. 怎样用SQL语句查询一个数据库中的所有表?

    怎样用SQL语句查询一个数据库中的所有表?  --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...

  8. SQL语句查询某表的所有字段及数据类型

    SQL语句查询某表的所有字段及数据类型 SELECT name AS column_name , TYPE_NAME(system_type_id) AS column_type , max_leng ...

  9. sql语句查询数据库表结构信息

    开发中经常用到查询指定表及其字段的信息,以下是我整理的SQL语句查询方法,供自己平时使用也提供给大家参考! 1.适用MS SQL SERVER: SELECT 表名 = then d.name els ...

随机推荐

  1. Unity3D加密保护解决方案

    精锐5加密锁支持Unity3D代码及资源保护,并提供授权方案 产品简介 可使用Virbox Protector加壳工具对Unity3D代码进行加密.Unity3D使用开源mono C#语法,代码会编译 ...

  2. Redis入门到高可用(十九)——Redis Sentinel

    一.Redis  Sentinel架构     二.redis sentinel安装与配置 四.客户端连接Sentinel            四.实现原理—— 故障转移演练(客户端高可用) 五.实 ...

  3. jsp一些使用技巧

    1.web.xml中配置error页面 一.<error-page> <error-code>500</error-code> <location>50 ...

  4. 数据库SQL的分组函数

    分组函数:(五个) 1···max(expr):求expr的最大值 }\ 2···min(expr):求expr的最小值 }-- 数据类型是有规定的 3···sum(expr):求expr的总和 }- ...

  5. kali蓝牙渗透

    1.hcitool 通过前面讲的升级操作后,在BackTrack4 Linux或者Ubuntu系统下将会安装好蓝牙的全套操作工具,其中就包括hcitool.该工具支持大量的蓝牙设备操作,从扫描到查看设 ...

  6. 2019/4/11 wen 常用类2

  7. 系统安装后的linux和vmware的网络配置

    一.1表示linux的版本   2表示linux内核的版本   3表示操作系统多少位  4.表示操作系统的名称 二.配置虚拟机网络 1.在办公室控制和使用服务器(机房) 2.远程连接 ###配置虚拟机 ...

  8. Bugku-CTF之flag在index里

      Day15 flag在index里 http://123.206.87.240:8005/post/      

  9. redux-thunk的理解

    问题:1.redux-thunk要解决什么问题? 要解决异步请求问题,Action发出以后,Reducer立即算出State,这叫做同步:Action发出以后,过一段时间再执行 Reducer,这就叫 ...

  10. Code Pages

    https://docs.microsoft.com/en-us/windows/desktop/intl/code-pages Most applications written today han ...