码迷,mamicode.com
首页 > 数据库 > 详细

UNION函数的使用以及SQL文的高效编辑方法

时间:2018-06-05 11:27:57      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:back   需要   语句   rac   closed   opened   查询   高级   span   

查找SELECT 相同列的情况下,例如选择一个月每一天的某几项数据,相当于查询一个月三十行数据,为了在一个表中显示就要使用union函数。

eg:

技术分享图片
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-01% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-02% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-03% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-04% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-05% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-06% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-07% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-08% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-09% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-10% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-11% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-12% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-13% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-14% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-15% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-16% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-17% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-18% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-19% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-20% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-21% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-22% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-23% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-24% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-25% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-26% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-27% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-28% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-29% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-30% AND act = 1005001001 UNION ALL
SELECT COUNT(DISTINCT uid) AS c FROM da_track WHERE data_time LIKE 2018-05-31% AND act = 1005001001
View Code

1. UNION 表示去重的结果;

2. UNION ALL 表示不进行去重的结果;

 UNION函数就是用来拼表的,条件是每个选择语句的列相同,这个列的内容可能相同或者不同,不同的时候以第一个选择语句为准。

需求在哪?比如要使用拼接之后的表格,就很有用了,场景:一个月30天,求每一天去重的日活量,然后求和再做平均,这就需要将去重的日活拼接成一张30行的表,然后再做SUM然后再求平均。

根据实际需要使用UNION函数,使用SQL最重要的技能就是理解表格之间的关系,前后层次,你需要什么表格就使用连接JOIN、UNION等方法人为实现什么表格。

在重复查询的时候,一般都是修改一段SQL文的小部分语句,比如日期的天的数字,这种情况就需要掌握高级编辑方法来提高编辑效率,使用excel和高级文本编辑器(editplus等)以及正则化手段

方法总结:

 

UNION函数的使用以及SQL文的高效编辑方法

标签:back   需要   语句   rac   closed   opened   查询   高级   span   

原文地址:https://www.cnblogs.com/xuhaojun/p/9138513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!