MySQL,如何使用join三表插入新数据表

时间:2018-04-27 04:22:02

标签: mysql

我有三个这样的数据表

---------------------------------------
id       price        type       
---------------------------------------
1         10          a001       
2         20          a002       
3         30          a003      

二月

---------------------------------------
id       price        type       
---------------------------------------
1         20          a001       
2         15          a002       
3         18          a003       
4         33          a004       

---------------------------------------
id       price        type       
---------------------------------------
1         16          a001       
2         40          a002       
3         25          a004       
4         51          a005       

我需要加入以上三个数据来插入我的新表

-------------------------------------------------------------------------
id     jan.price   feb.price   mar.price  jan.type   feb.type   mar.type
-------------------------------------------------------------------------
1      10          20          16         a001       a001       a001
2      20          15          40         a002       a002       a002
3      30          18                     a003       a003
4                  33          25                    a004       a004
5                              51                               a005
新表中的

id 是自动增量,而不是上面的三个。

请帮我查询MySQL语法。

3 个答案:

答案 0 :(得分:2)

尝试使用以下查询(第一个变体是错误的)

SELECT
  id,
  SUM(jan_price) jan_price,SUM(feb_price) feb_price,SUM(mar_price) mar_price,
  MAX(jan_type) jan_type,MAX(feb_type) feb_type,MAX(mar_type) mar_type
FROM
  (
    SELECT id,price jan_price,NULL feb_price,NULL mar_price,type jan_type,NULL feb_type,NULL mar_type
    FROM Jan

    UNION ALL

    SELECT id,NULL jan_price,price feb_price,NULL mar_price,NULL jan_type,type feb_type,NULL mar_type
    FROM Feb

    UNION ALL

    SELECT id,NULL jan_price,NULL feb_price,price mar_price,NULL jan_type,NULL feb_type,type mar_type
    FROM Mar
  ) q
GROUP BY id

SQL小提琴 - http://www.sqlfiddle.com/#!9/bd9d2a/1

数据按类型排序的第二个变体(id是使用@id变量生成的)

SELECT
  t.id,
  m1.price jan_price,
  m2.price feb_price,
  m3.price mar_price,
  m1.type jan_type,
  m2.type feb_type,
  m3.type mar_type
FROM
  (
    SELECT @id:=@id+1 id,type
    FROM
      (
        SELECT type
        FROM Jan
        UNION
        SELECT type
        FROM Feb
        UNION
        SELECT type
        FROM Mar
      ) t,
      (SELECT @id:=0) n
    ORDER BY type
  ) t
LEFT JOIN Jan m1 ON m1.type=t.type
LEFT JOIN Feb m2 ON m2.type=t.type
LEFT JOIN Mar m3 ON m3.type=t.type
ORDER BY t.id

SQL小提琴 - http://www.sqlfiddle.com/#!9/bd9d2a/23

如果要将其放入带有自动增量字段的新表中,则需要以下

CREATE TABLE SumTable(
  id int not null auto_increment primary key,
  jan_price int,
  feb_price int,
  mar_price int,
  jan_type varchar(10),
  feb_type varchar(10),
  mar_type varchar(10)
);

INSERT SumTable(jan_price,feb_price,mar_price,jan_type,feb_type,mar_type)
SELECT
  m1.price jan_price,
  m2.price feb_price,
  m3.price mar_price,
  m1.type jan_type,
  m2.type feb_type,
  m3.type mar_type
FROM
  (
    SELECT type
    FROM Jan
    UNION
    SELECT type
    FROM Feb
    UNION
    SELECT type
    FROM Mar
  ) t
LEFT JOIN Jan m1 ON m1.type=t.type
LEFT JOIN Feb m2 ON m2.type=t.type
LEFT JOIN Mar m3 ON m3.type=t.type
ORDER BY t.type;

SELECT *
FROM SumTable

type1 and type2

的情况

测试数据

CREATE TABLE Jan(id int,price int,type1 varchar(10),type2 varchar(10));
INSERT Jan(id,price,type1,type2)VALUES
(1,10,'a001','b002'), 
(2,20,'a002','b001'), 
(3,30,'a003','b003');

CREATE TABLE Feb(id int,price int,type1 varchar(10),type2 varchar(10));
INSERT Feb(id,price,type1,type2)VALUES
(1,20,'a001','b001'),   
(2,15,'a002','b001'),   
(3,18,'a003','b003'),   
(4,33,'a004','b003');

CREATE TABLE Mar(id int,price int,type1 varchar(10),type2 varchar(10));
INSERT Mar(id,price,type1,type2)VALUES
(1,16,'a001','b002'),
(2,40,'a002','b002'),
(3,25,'a004','b002'),
(4,51,'a005','b002');

结果表

CREATE TABLE SumTable(
  id int not null auto_increment primary key,
  jan_price int,
  feb_price int,
  mar_price int,
  jan_type1 varchar(10),
  feb_type1 varchar(10),
  mar_type1 varchar(10),
  jan_type2 varchar(10),
  feb_type2 varchar(10),
  mar_type2 varchar(10)
);

查询

INSERT SumTable(jan_price,feb_price,mar_price,jan_type1,feb_type1,mar_type1,jan_type2,feb_type2,mar_type2)
SELECT
  m1.price jan_price,
  m2.price feb_price,
  m3.price mar_price,
  m1.type1 jan_type1,
  m2.type1 feb_type1,
  m3.type1 mar_type1,
  m1.type2 jan_type1,
  m2.type2 feb_type1,
  m3.type2 mar_type1
FROM
  (
    SELECT type1,type2
    FROM Jan
    UNION
    SELECT type1,type2
    FROM Feb
    UNION
    SELECT type1,type2
    FROM Mar
  ) t
LEFT JOIN Jan m1 ON m1.type1=t.type1 AND m1.type2=t.type2
LEFT JOIN Feb m2 ON m2.type1=t.type1 AND m2.type2=t.type2
LEFT JOIN Mar m3 ON m3.type1=t.type1 AND m3.type2=t.type2
ORDER BY t.type1,t.type2;

结果

SELECT *
FROM SumTable

SQL小提琴 - http://www.sqlfiddle.com/#!9/be68ed/5

答案 1 :(得分:0)

-------------------------------------------------------------------------
id     jan.price   feb.price   mar.price  jan.type   feb.type   mar.type
-------------------------------------------------------------------------
1      10          20          16         a001       a001       a001
2      20          15          40         a002       a002       a002
3      30          18                     a003       a003
4                  33          25                    a004       a004
5                              51                               a005

要获得上述结果,您必须修改ID错误的第三个表(Mar表)。

请参考:  SQL小提琴 - http://www.sqlfiddle.com/#!9/680ab2/2

答案 2 :(得分:0)

你可以使用连接来做到这一点:

select  b.id,a.price as jan_price,b.price as feb_price,
        c.price as march_price,
        a.type as jan_type,
        b.type as feb_type,c.type as march_type
from feb b 
left join jan a on a.type = b.type 
inner join march c on c.id =b.id

SQL FIDDLE:http://sqlfiddle.com/#!9/8898b3/35