在两个表上分组,然后附加结果并删除重复项

时间:2019-01-31 13:31:04

标签: mysql

我有2个表,每个表中都有多列。从下面可以看出,列标题的名称不同。 我想在EACH表的第一列和第二列上执行GROUP BY,然后在输出中添加结果,并且不应重复。而且结果只需要前两列作为输出。

表名= STAT

Region   Brand  M08 
-----------------
P1       A      xx     
P1       A      yy    
P2       B      zz     
P2       C      yy      
P3       A      uu     
P3       B      ii    
P3       C      oo   

表名称= MI

RegDes   P4Brand  M08 
-----------------   
P2       B        er    
P2       C        tr     
P3       A        ty     
P3       B        ew    
P4       C        yu  
P4       A        tr     
P4       B        ty     
P5       C        yu 

表名= EXPECTED_OUTPUT

Region   Brand   
-----------------  
P1       A              
P2       B          
P2       C         
P3       A          
P3       B         
P4       C     
P4       A         
P4       B        
P5       C    

我需要一些帮助编写SQL脚本。我已经为3个表创建了架构。 最终输出将插入到表中:EXPECTED_OUTPUT

1 个答案:

答案 0 :(得分:1)

我将使用UNION ALL将两个表合并为一个唯一的表。然后,在该查询周围包裹一个SELECT *并应用一个GROUP BY

或者,除了使用GROUP BY之外,我也不会使用SELECT DISTINCT *

或者,如@Usagi Miyamoto在评论中所述,使用简单的UNION(不使用ALL

模式(MySQL v5.7)

CREATE TABLE stat (
  `Region` VARCHAR(2),
  `Brand` VARCHAR(1),
  `M08` VARCHAR(2)
);

INSERT INTO stat
  (`Region`, `Brand`, `M08`)
VALUES
  ('P1', 'A', 'xx'),
  ('P1', 'A', 'yy'),
  ('P2', 'B', 'zz'),
  ('P2', 'C', 'yy'),
  ('P3', 'A', 'uu'),
  ('P3', 'B', 'ii'),
  ('P3', 'C', 'oo');



CREATE TABLE mi (
  `RegDes` VARCHAR(2),
  `P4Brand` VARCHAR(1),
  `M08` VARCHAR(2)
);

INSERT INTO mi
  (`RegDes`, `P4Brand`, `M08`)
VALUES
  ('P2', 'B', 'er'),
  ('P2', 'C', 'tr'),
  ('P3', 'A', 'ty'),
  ('P3', 'B', 'ew'),
  ('P4', 'C', 'yu'),
  ('P4', 'A', 'tr'),
  ('P4', 'B', 'ty'),
  ('P5', 'C', 'yu');

查询#1 GROUP BY

SELECT * FROM 
(
    SELECT s.Region AS Region, s.Brand AS Brand
    FROM stat s
    UNION ALL
    SELECT m.RegDes AS Region, m.P4Brand AS Brand
    FROM mi m
) AS EXPECTED_OUTPUT
GROUP BY Region, Brand;

输出

| Region | Brand |
| ------ | ----- |
| P1     | A     |
| P2     | B     |
| P2     | C     |
| P3     | A     |
| P3     | B     |
| P3     | C     |
| P4     | A     |
| P4     | B     |
| P4     | C     |
| P5     | C     |

查询#2 DISTINCT

SELECT DISTINCT * FROM 
(
    SELECT s.Region AS Region, s.Brand AS Brand
    FROM stat s
    UNION ALL
    SELECT m.RegDes AS Region, m.P4Brand AS Brand
    FROM mi m
) AS EXPECTED_OUTPUT;

输出

| Region | Brand |
| ------ | ----- |
| P1     | A     |
| P2     | B     |
| P2     | C     |
| P3     | A     |
| P3     | B     |
| P3     | C     |
| P4     | C     |
| P4     | A     |
| P4     | B     |
| P5     | C     |

查询#3 UNION

SELECT s.Region AS Region, s.Brand AS Brand
FROM stat s
UNION
SELECT m.RegDes AS Region, m.P4Brand AS Brand
FROM mi m;

输出

| Region | Brand |
| ------ | ----- |
| P1     | A     |
| P2     | B     |
| P2     | C     |
| P3     | A     |
| P3     | B     |
| P3     | C     |
| P4     | C     |
| P4     | A     |
| P4     | B     |
| P5     | C     |

View on DB Fiddle


要将结果插入表中,可以使用INSERT INTO SELECT语句:

按查询分组

INSERT INTO EXPECTED_OUTPUT
SELECT * FROM 
(
    SELECT s.Region AS Region, s.Brand AS Brand
    FROM stat s
    UNION ALL
    SELECT m.RegDes AS Region, m.P4Brand AS Brand
    FROM mi m
) AS EXPECTED_OUTPUTDATAS
GROUP BY Region, Brand;

DISTINCT查询

INSERT INTO EXPECTED_OUTPUT
SELECT DISTINCT * FROM 
(
    SELECT s.Region AS Region, s.Brand AS Brand
    FROM stat s
    UNION ALL
    SELECT m.RegDes AS Region, m.P4Brand AS Brand
    FROM mi m
) AS EXPECTED_OUTPUTDATAS;

UNION查询

INSERT INTO EXPECTED_OUTPUT
SELECT s.Region AS Region, s.Brand AS Brand
FROM stat s
UNION
SELECT m.RegDes AS Region, m.P4Brand AS Brand
FROM mi m;