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 ...
随机推荐
- WebRTC 基于GCC的拥塞控制(上)
转自:http://blog.csdn.net/doitsjz/article/details/56481981 实时流媒体应用的最大特点是实时性,而延迟是实时性的最大敌人.从媒体收发端来讲,媒体数据 ...
- Unity3d-Particle System系统的学习(一)
最近看了下Unity3d的粒子系统的相关视频,并且动手操作了下,感觉自己的美工技能又增进了下(开个小玩笑),发现粒子系统所需要记忆的东西还是有点多的. 所以为了不让自己遗忘某些知识点,我准备发布成博客 ...
- 每天定时备份mysql数据库任务
需求: 1,每天4点备份mysql数据: 2,为节省空间,删除超过3个月的所有备份数据: 3,删除超过7天的备份数据,保留3个月里的 10号 20号 30号的备份数据: #创建shell文件 vim ...
- ios之归档demo
ios对自定义对象的归档.首先需要实现NSCoding与NSCopying接口 #import <Foundation/Foundation.h> @interface Person : ...
- EditText 限制输入,自定义样式,监听输入的字符,自动换行
自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...
- Eclipse设置方法模板
我们在项目中进行代码书写的时候,对个人所写的代码打上个人的标签的话,这样便于后期代码的维护跟踪,好处多多. 设置注释模板的入口: Window->Preference->Java-> ...
- #incldue<cctype>函数系列
#include <cctype>的函数 c++中应该是#include <cctype> c中应该是#include <ctype.h> 以下为字符函数库中常用的 ...
- 从客户端(SeekingJobs="<B>·</B> 物流规划,<B>·</...")中检测到有潜在危险的 Request.Form 值。
今天提交各东西发觉出错了,错误信息如下: “/”应用程序中的服务器错误. 从客户端(SeekingJobs="<B>·</B> 物流规划,<B>·< ...
- [leetcode]Triangle @ Python
原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...
- 根据ip地址获取用户所在地
java代码: package com.henu.controller; import java.io.BufferedReader; import java.io.IOException; impo ...