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

Posted nichoo的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UNION函数的使用以及SQL文的高效编辑方法相关的知识,希望对你有一定的参考价值。

查找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文的高效编辑方法的主要内容,如果未能解决你的问题,请参考以下文章

在具有相同架构的表上使用 UNION ALL 的 Google Big Query 中的 SQL 错误编辑:架构从 String 更改为 INT

sql union和union all的用法及效率 以及与join的区别

SQL Server-聚焦UNIOL ALL/UNION查询

关于SQL 查询效率问题 left join 改成 inner join union

SQL注入 union和select替换为空绕过

mysql中Mysql模糊查询like效率,以及更高效的写法和sql优化方法