Mysql的union
我同事写了一条Sql语句,当时没看明白,下面晒出来:
SELECT SUM(new_cart) as new_cart,SUM(new_collect) as new_collect, SUM(total_cart) as total_cart, SUM(total_collect) as total_collect, SUM(ipv) ipv, SUM(iuv) iuv FROM
( SELECT SUM(new_cart) as new_cart,SUM(new_collect) as new_collect, as total_cart, as total_collect, SUM(ipv) ipv, SUM(iuv) iuv FROM tablename WHERE id <= 5 union all SELECT as new_cart, as new_collect,total_cart, total_collect, AS ipv, AS iuv FROM tablename WHERE id = 5
) as t
ORDER BY new_cart desc;
数据表结构:
CREATE TABLE `tablename` (
`id` int() NOT NULL AUTO_INCREMENT,
`new_cart` bigint() DEFAULT NULL,
`new_collect` bigint() DEFAULT NULL,
`total_cart` bigint() DEFAULT NULL ,
`total_collect` bigint() DEFAULT NULL ,
`ipv` bigint() DEFAULT NULL ,
`iuv` bigint() DEFAULT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
那么其实是使用了MYSQL的UNION。UNION:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
录入语句:
mysql> insert into tablename values (,,,,,,);
Query OK, row affected (0.00 sec) mysql> insert into tablename values (,,,,,,);
Query OK, row affected (0.00 sec) mysql> insert into tablename values (,,,,,,);
Query OK, row affected (0.00 sec) mysql> insert into tablename values (,,,,,,);
Query OK, row affected (0.00 sec) mysql> insert into tablename values (,,,,,,);
Query OK, row affected (0.00 sec)
执行第一条语句:
SELECT SUM(new_cart) as new_cart,SUM(new_collect) as new_collect, as total_cart, as total_collect, SUM(ipv) ipv, SUM(iuv) iuv FROM tablename WHERE id <= union all SELECT as new_cart, as new_collect,total_cart, total_collect, AS ipv, AS iuv FROM tablename WHERE id = ;
输出:
+----------+-------------+------------+---------------+------+------+
| new_cart | new_collect | total_cart | total_collect | ipv | iuv |
+----------+-------------+------------+---------------+------+------+
| | | | | | |
| | | | | | |
+----------+-------------+------------+---------------+------+------+
rows in set (0.00 sec)
可见,union是把两条记录合并输出出来,那么再将这些内容相加输出该怎么办哪?聪明的你一定想到了第一条数据语句了吧~
mysql> SELECT SUM(new_cart) as new_cart,SUM(new_collect) as new_collect, SUM(total_cart) as total_cart, SUM(total_collect) as total_collect, SUM(ipv) ipv, SUM(iuv) iuv FROM ( SELECT SUM(new_cart) as new_cart,SUM(new_collect) as new_collect, as total_cart, as total_collect, SUM(ipv) ipv, SUM(iuv) iuv FROM tablename WHERE id <= union all SELECT as new_cart, as new_collect,total_cart, total_collect, AS ipv, AS iuv FROM tablename WHERE id = ) as t ORDER BY new_cart desc;
+----------+-------------+------------+---------------+------+------+
| new_cart | new_collect | total_cart | total_collect | ipv | iuv |
+----------+-------------+------------+---------------+------+------+
| | | | | | |
+----------+-------------+------------+---------------+------+------+
row in set (0.00 sec)
Mysql的union的更多相关文章
- 【转】mysql的union、left join、 right join、 inner join和视图学习
1.联合 union 进行多个查询语句时,要求多次查询的结果列数必须一样.此时,查询的结果以第一个sql语句的列名为准且union会自动去重复我们应该使用union all. 例...... 1.联合 ...
- MySQL中union和order by一起使用的方法
MySQL中union和order by是可以一起使用的,但是在使用中需要注意一些小问题,下面通过例子来说明.首先看下面的t1表. 1.如果直接用如下sql语句是会报错:Incorrect usage ...
- 阳性比例 mysql CASE UNION ALL
阳性比例 mysql CASE UNION ALL SELECT t.*,t.type_0/all_ FROM ( SELECT FROM_UNIXTIME(create_time,'%Y-%m-%d ...
- MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)
我在一个业务中采用了按月的分表策略,当查询的条件跨月的时候,使用了union all汇总2个表的数据,并按插入时间倒序排列.查询并不复杂,但是当执行的时候却报错了. SELECT * FROM `ta ...
- Mysql中Union和OR性能对比
博客已搬家,更多内容查看https://liangyongrui.github.io/ Mysql中Union和OR性能对比 在leetcode上看到一篇文章,整理一下 参考:https://leet ...
- MySQL使用Union创建视图报错
mysql> select * from test_main; +----+-------+ | id | value | +----+-------+ | 1 | ONE | | 2 ...
- MySQL中UNION和UNION ALL的使用
在数据库中,UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同. MySQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后 ...
- mysql中UNION ALL用法
MYSQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果. 举例说明: select * from table1 u ...
- Mysql下Union注入Bypass安全狗过程
文章转载于:http://www.0aa.me/index.php/archives/95/ 一次众测发现个注入,然后有安全狗就顺带看了下安全狗. 先fuzz看看安全狗拦截什么关键词union sel ...
随机推荐
- 无线通信中FEC 编码原理及评价
转自:http://blog.csdn.net/wiznet2012/article/details/7492146 大家好,前面我们给大家介绍了无线通信中FEC编码原理(1)和(2),今天继续献上F ...
- SharePoint Online 创建网站集
前言 本文介绍如何在Office 365中创建SharePoint网站集. 正文 通过登录地址登录到Office 365环境中,我们可以在左上角的按钮中点开,进入管理员,也可以直接在页面中点击管理: ...
- JavaScript中 location.host 与 location.hostname 的区别
JavaScript 中,大多数情况下,我们不会发现 location.host 与 location.hostname 的区别,因为大多数情况下,我们的网页用的是 80 端口. 他们的区别: loc ...
- 低版本系统兼容的ActionBar(七)自定义Actionbar标题栏字体
这个自定义字体其实和ActionBar有关,但之前写AtionBar的时候没考虑到修改字体样式,今天看到一篇专门写这个的文章就贴上使用方式.╮(╯▽╰)╭,不得不说Actionbar的那个样式真是让人 ...
- SVG 使用marker画箭头(一)
一.使用Marker画箭头 1.定义一个箭头的marker引用 <defs> <marker id='markerArrow' markerWidth='13' markerHeig ...
- cannot import name 'main' 解决方案
error description: pip3 install numpy Traceback (most recent call last): File "/usr/bin/pip3&qu ...
- Intellij IDEA打开就闪退或关闭
找到idea安装目录的bin目录,搜索vmoptions可以看到两个文件, idea.exe.vmoptions idea64.exe.vmoptions 1 这两个文件就是IDEA的一些配置文件,带 ...
- C/C++ 关于 for循环 的第二个表达式右侧非常量的时候
废话不多说,直接看代码: #include<stdio.h> int main(){ ; ;z<zmax;z++){ printf("i=%d z=%d\n",i ...
- libc中的标准函数 localtime和localtime_r 的用法
http://baike.baidu.com/view/1080853.htm 随便一查,就可以查到基本用法,但是... http://blog.csdn.net/maocl1983/article/ ...
- Solr4.4入门,介绍Solr的安装、IK分词器的配置及高亮查询结果(转)
一.Windows下安装solr-4.4.0 1. 下载solr.4.4 2. 下载绿色版tomcat6.0.18 3. 解压下载的solr到d:\study\solr,将dist目录下的sol ...