查找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'
1. UNION 表示去重的结果;
2. UNION ALL 表示不进行去重的结果;
UNION函数就是用来拼表的,条件是每个选择语句的列相同,这个列的内容可能相同或者不同,不同的时候以第一个选择语句为准。
需求在哪?比如要使用拼接之后的表格,就很有用了,场景:一个月30天,求每一天去重的日活量,然后求和再做平均,这就需要将去重的日活拼接成一张30行的表,然后再做SUM然后再求平均。
根据实际需要使用UNION函数,使用SQL最重要的技能就是理解表格之间的关系,前后层次,你需要什么表格就使用连接JOIN、UNION等方法人为实现什么表格。
在重复查询的时候,一般都是修改一段SQL文的小部分语句,比如日期的天的数字,这种情况就需要掌握高级编辑方法来提高编辑效率,使用excel和高级文本编辑器(editplus等)以及正则化手段。
方法总结:
1. 写好第一句SQL文;
2. 使用excel拼接SQL文,即复制重复部分,利用excel自动生成不重复部分(比如日期自动+1);
3. 使用文本编辑器的正则化功能实现SQL文的修饰。
注意:使用Ctrl + D 来复制(向下填充)Ctrl + R 向右填充;修改excel单元格格式;excel的CONCATENATE()函数可以拼接多个字符串;
学会使用excel的单元格引用,$B3中的$符号表示绝对引用B列,非绝对引用3行,行数可以拖动自动改变,用来引用单元格的内容,这在需要使用单元格内容的SQL文中很重要。
editplus 使用 Ctrl + H 进入正则化替换。