我同事写了一条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的更多相关文章

  1. 【转】mysql的union、left join、 right join、 inner join和视图学习

    1.联合 union 进行多个查询语句时,要求多次查询的结果列数必须一样.此时,查询的结果以第一个sql语句的列名为准且union会自动去重复我们应该使用union all. 例...... 1.联合 ...

  2. MySQL中union和order by一起使用的方法

    MySQL中union和order by是可以一起使用的,但是在使用中需要注意一些小问题,下面通过例子来说明.首先看下面的t1表. 1.如果直接用如下sql语句是会报错:Incorrect usage ...

  3. 阳性比例 mysql CASE UNION ALL

    阳性比例 mysql CASE UNION ALL SELECT t.*,t.type_0/all_ FROM ( SELECT FROM_UNIXTIME(create_time,'%Y-%m-%d ...

  4. MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)

    我在一个业务中采用了按月的分表策略,当查询的条件跨月的时候,使用了union all汇总2个表的数据,并按插入时间倒序排列.查询并不复杂,但是当执行的时候却报错了. SELECT * FROM `ta ...

  5. Mysql中Union和OR性能对比

    博客已搬家,更多内容查看https://liangyongrui.github.io/ Mysql中Union和OR性能对比 在leetcode上看到一篇文章,整理一下 参考:https://leet ...

  6. MySQL使用Union创建视图报错

    mysql> select * from test_main; +----+-------+ | id | value | +----+-------+ |  1 | ONE   | |  2  ...

  7. MySQL中UNION和UNION ALL的使用

    在数据库中,UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同. MySQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后 ...

  8. mysql中UNION ALL用法

    MYSQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果. 举例说明: select * from table1 u ...

  9. Mysql下Union注入Bypass安全狗过程

    文章转载于:http://www.0aa.me/index.php/archives/95/ 一次众测发现个注入,然后有安全狗就顺带看了下安全狗. 先fuzz看看安全狗拦截什么关键词union sel ...

随机推荐

  1. 查询EBS系统在线人数

    /* Formatted on 2018/3/14 23:25:51 (QP5 v5.256.13226.35538) */ SELECT U.USER_NAME , APP.APPLICATION_ ...

  2. 【spring cloud】【spring boot】网管服务-->配置文件添加endpoints.enabled = false,SpringBoot应用监控Actuator使用的安全隐患

    转载:https://xz.aliyun.com/t/2233 ==================================================================== ...

  3. Fidder发送Get、POST请求

      Composer:   1.Get请求 a) 请求头加上: Content-Type: application/json; charset=utf-8   b) url:http://localh ...

  4. ProgressBar学习笔记,自定义横向进度条的样式(包含ActionBar上面的进度条)

     点显示进度条后→   android:max="100" 进度条的最大值 android:progress  进度条已经完成的进度值 android:progressDrawab ...

  5. TeXLive里面集成了CTeX,Lyx是一个编辑软件

    TeXLive里面集成了CTeX,LaTeX是排版引擎,CTeX和TeXLive是发行版.LaTeX是毛坯房,CTeX和TeX Live是带精装的房子.CTeX 套装是一个个人作品,存在很多问题.这些 ...

  6. tcp常见状态

    常见状态 1.建立连接 2.关闭连接

  7. idea自动生成serialVersionUID , serialVersionUID的作用

    Java的序列化的机制通过判断serialVersionUID来验证版本的一致性.在反序列化的时候与本地的类的serialVersionUID进行比较,一致则可以进行反序列化,不一致则会抛出异常Inv ...

  8. Leap Motion 上手体验

    很早之前就关注了Leap Motion这个颠覆性的体感操作设备,如今7月22日上市至今已经一个月左右,淘宝的价格也已经降到650元,虽说相对国外还是偏贵,但是已经忍不住尝尝鲜了. Leap Motio ...

  9. libxml2 使用教程【转】

    https://blog.csdn.net/zhoudaxia/article/details/8565731# 本文整理自官方使用教程http://xmlsoft.org/tutorial/inde ...

  10. C# 同一应用程序域不同线程之间的参数传递方式

    很久没有写博客了,最近的项目不用写代码.今天没事就看看thread之间的参数传递方式,这里主要适用于运行在不同线程的两个方法之间参数传递.直接看代码 1.方法之间直接传递参数 void DemoPar ...