系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 数据库 > Mysql > 详细页面

Mysql中使用Union—多表合并之行合并

时间:2023-10-29来源:系统城装机大师作者:佚名

Union (all)语句格式

select 列名    from 表A

union (all)

select 列名    from 表B

注意事项:

  • 合并的表的列数必须是一致的,也就是表的列数要相同
  • union 与union all 对两个查询结果合并的时候,如果结果有重复,union 会把重复的值删除;
  • union 与union all不能与order by同时使用,如果要对结果进行排序,可以用子查询

案例解析

统计20170703—20170709周内每天及本周累计销售金额、订单量、会员数、订单占比

  • 统计时间段内每天的累计销售金额、订单量、会员数
  • 统计本周累计销售金额、订单量、会员数
  • 订单占比
  • union合并表
  • 对比 union all合并表——因为没有重复数据,所以两表合并结果一样
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- 1、统计时间段内每天的累计销售金额、订单量、会员数
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W') ;
-- 2、统计本周累计销售金额、订单量、会员数
SELECT SUM(AMT) as total_money
      ,COUNT(DISTINCT salesID) as total_num_order
      ,COUNT(DISTINCT dimMemberID) total_num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0;
-- 3、订单占比
SELECT DATE_FORMAT(dimDateID,'%W') AS week_1
      ,CONCAT(ROUND( COUNT(DISTINCT salesID)/(SELECT COUNT(DISTINCT salesID)
                                FROM dw.fct_sales
                                where dimDateID between '20170703' and '20170709'
                                and dimMemberID <>0),4)*100,'%') as order_rate
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W') ;
-- 4、union合并表
SELECT DATE_FORMAT(dimDateID,'%W') AS week_1
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order
      ,COUNT(DISTINCT dimMemberID) num_member
      ,CONCAT(ROUND( COUNT(DISTINCT salesID)/(
       SELECT COUNT(DISTINCT salesID)
       FROM dw.fct_sales
       where dimDateID between '20170703' and '20170709'
       and dimMemberID <>0),4)*100,'%') as order_rate
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W')
UNION
SELECT week(dimDateID,1)               /*为了保证列数一样,week返回日期为一年中的第几周 weel(date,1):从周一开始为第一天*/
      ,SUM(AMT) as total_money
      ,COUNT(DISTINCT salesID) as total_num_order
      ,COUNT(DISTINCT dimMemberID) total_num_member
      ,'100%' as total                 /*为确保列数一样*/
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by week(dimDateID,1);            /*出现汇总函数需要进行分组*/
-- 5、对比 union all合并表——因为没有重复数据,所以两表合并结果一样
SELECT DATE_FORMAT(dimDateID,'%W') AS week_1
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order
      ,COUNT(DISTINCT dimMemberID) num_member
      ,CONCAT(ROUND( COUNT(DISTINCT salesID)/(
       SELECT COUNT(DISTINCT salesID)
       FROM dw.fct_sales
       where dimDateID between '20170703' and '20170709'
       and dimMemberID <>0),4)*100,'%') as order_rate
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W')
UNION ALL
SELECT week(dimDateID,1)               /*为了保证列数一样,week返回日期为一年中的第几周 weel(date,1):从周一开始为第一天*/
      ,SUM(AMT) as total_money
      ,COUNT(DISTINCT salesID) as total_num_order
      ,COUNT(DISTINCT dimMemberID) total_num_member
      ,'100%' as total                 /*为确保列数一样*/
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by week(dimDateID,1);            /*出现汇总函数需要进行分组*/
区分union 和 union all ,利用重复数据对比,合并两个一模一样的表 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- union
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W')
UNION
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W') ;
-- union all
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W')
UNION ALL
SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
      ,SUM(AMT) as money
      ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
      ,COUNT(DISTINCT dimMemberID) num_member
FROM dw.fct_sales
where dimDateID between '20170703' and '20170709'
and dimMemberID <>0
group by DATE_FORMAT(dimDateID,'%W') ;
多表合并中的排序问题 order by——把合并后的表作为一个临时表,再进行排序

注意临时表需要命名

排序要对新的列名进行排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
SELECT sn.*
FROM (
      SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
            ,SUM(AMT) as money
            ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
            ,COUNT(DISTINCT dimMemberID) num_member
      FROM dw.fct_sales
      where dimDateID between '20170703' and '20170709'
      and dimMemberID <>0
      group by DATE_FORMAT(dimDateID,'%W')
      UNION
      SELECT DATE_FORMAT(dimDateID,'%W') as week_1      /*把时间转化为星期模式*/
            ,SUM(AMT) as money
            ,COUNT(DISTINCT salesID) as num_order       /*一个会员一周可以下多单,所以要统计会员数,需要去重*/
            ,COUNT(DISTINCT dimMemberID) num_member
      FROM dw.fct_sales
      where dimDateID between '20170703' and '20170709'
      and dimMemberID <>0
      group by DATE_FORMAT(dimDateID,'%W') ) as sn
order by money desc;                                    /*注意要对money排序,而不是SUM(AMT)*/
-- order by SUM(AMT) desc;                              /*会报错,需要用临时表的列名,并且要对临时表取列名,否则也会报错*/

到此这篇关于Mysql中使用Union—多表合并之行合并的文章就介绍到这了

分享到:

相关信息

  • MySQL的核心查询语句详解

    一、单表查询 1、排序 2、聚合函数 3、分组 4、limit 二、SQL约束 1、主键约束 2、非空约束 3、唯一约束 4、外键约束 5、默认值 三、多表查询 1、内连接 1)隐式内连接: 2)显式内连接: 2、外连接 1)左外连接 2)右外连接 四...

    2023-10-30

  • Mysql中如何删除表重复数据

    Mysql删除表重复数据 表里存在唯一主键 没有主键时删除重复数据 Mysql删除表中重复数据并保留一条 准备一张表 用的是mysql8 大家自行更改 创建表并添加四条相同的数据...

    2023-10-30

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载