sql语句查询结果合并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语句查询结果合并union all用法
整理别人的sql 大概的思想是用union 和union all --合并重复行select * from Aunion select * from B --不合并重复行select * from A ...
- 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_ ...
- 使用sql语句查询日期在一定时间内的数据
使用sql语句查询日期在一周内的数据 select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查询当天日期在一周年 ...
- 浅谈MySQL中优化sql语句查询常用的30种方法 - 转载
浅谈MySQL中优化sql语句查询常用的30种方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使 ...
- sql语句查询
1. sql语句查询某位数字或者某几位数字开头的数据,字段类型为数字类: %’: 2. sql搜索以4开头和含有李字的数据: select * from wlzbpre_user where real ...
- phpcmsv9自定义sql语句查询模型实现
在phpcmsv9中,自定义sql语句查询可不太好实现,传入sql语句查询很容易被内部转入生成一系列莫名其妙的sql语句,比如最佳前缀等等,直接造成sql语句查询错误,在此也提供两种解决办法,1修改底 ...
- 怎样用SQL语句查询一个数据库中的所有表?
怎样用SQL语句查询一个数据库中的所有表? --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...
- SQL语句查询某表的所有字段及数据类型
SQL语句查询某表的所有字段及数据类型 SELECT name AS column_name , TYPE_NAME(system_type_id) AS column_type , max_leng ...
- sql语句查询数据库表结构信息
开发中经常用到查询指定表及其字段的信息,以下是我整理的SQL语句查询方法,供自己平时使用也提供给大家参考! 1.适用MS SQL SERVER: SELECT 表名 = then d.name els ...
随机推荐
- Java基础01-集合1、泛型
集合.泛型 第一章:集合1 1. 什么是集合 定义:在Java中,集合是一种可以存储多个数据的容器. 代码: ArrayList<String> list = new ArrayList& ...
- js中const,var,let区别
1.const定义的变量不可以修改,而且必须初始化(常量) const b = 5 // 正确 // const b // 错误,必须初始化 // b = 4 // 错误,不可被修改 console. ...
- C# 计时周期数(Ticks)在不同数据库上的实现
要在数据库中实现 DateTime.Ticks,先来看看 Ticks 在微软官方文档上的定义: 注解 一个计时周期表示一百纳秒,即一千万分之一秒. 毫秒内有 , 个计时周期,即 秒内有 , 万个计时周 ...
- API gateway 之 kong 安装 (二)
一.系统环境 [root@kong ~]# service iptables status iptables: Firewall is not running. [root@kong ~]# gete ...
- IndentationError: unindent does not match any outer indentation level笔记
执行一个Python脚本的时候,报"IndentationError: unindent does not match any outer indentation level" 错 ...
- VS环境下基于C++的单链表实现
------------恢复内容开始------------ #include<iostream> using namespace::std; typedef int ElemType; ...
- C/C++ return *this和return this的区别
首先我们知道~ class Test { public: Test() { return this; //返回的当前对象的地址 } Test&() { return *this; //返回的是 ...
- hack the box -- sizzle 渗透过程总结,之前对涉及到域内证书啥的还不怎么了解
把之前的笔记搬运过来 --- 1 开了443,用smbclient建立空连接查看共享 smbclient -N -L \\\\1.1.1.1 Department Shares Operatio ...
- [考试反思]1105csp-s模拟测试101: 临别
先不改题,这次主要不在T3上. 这次有必要粘文件得分了. 临考前总解锁新锅我也不知道这是什么个事啊... T1宏定义写挂.因为原来在OJ上没事所以一直没注意.在Lemon评测下直接全部RE. GG在主 ...
- P4568 [JLOI2011]飞行路线(分层图)
机房dalao推荐写的...(标签分层图) 经过前几题的分层图的洗礼,我深刻地体会到了分层图的优点和好处(主要是不想打dp....) 先说题吧.... 很明确,模型是最短路,但是,怎么跑k个,是个问题 ...