three表插入新的数据表

mec1mxoz  于 2021-06-23  发布在  Mysql
关注(0)|答案(3)|浏览(355)

我有三个这样的数据表

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

2月

---------------------------------------
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语法。

fdx2calv

fdx2calv1#

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

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号
如果您想将它放入一个带有autoincrement字段的新表中,您需要

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号

rm5edbpk

rm5edbpk2#

可以使用联接来执行此操作:

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小提琴:http://sqlfiddle.com/#!9月8898b3日

gmxoilav

gmxoilav3#

-------------------------------------------------------------------------
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

要得到上述结果,您必须修改第三个表(mar表),其中的id是错误的。
请参阅:sql fiddle-http://www.sqlfiddle.com/#!9/680ab2/2号

相关问题