Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL,如何用join三表插入新数据表_Mysql - Fatal编程技术网

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

MySQL,如何用join三表插入新数据表,mysql,Mysql,我有三个这样的数据表 一月 二月 玷污 我需要连接上面的三个数据才能像这样插入我的新表 总数 新表中的id是自动递增的,而不是从上述三个值开始递增 请帮助我查询MySQL语法。尝试使用以下查询(第一个变量错误) SQL小提琴- 第二个变量,其中数据按类型排序(id使用@id变量生成) SQL小提琴- 如果要将其放入具有autoincrement字段的新表中,则需要以下内容 CREATE TABLE SumTable( id int not null auto_increment primar

我有三个这样的数据表

一月

二月

玷污

我需要连接上面的三个数据才能像这样插入我的新表

总数

新表中的id是自动递增的,而不是从上述三个值开始递增


请帮助我查询MySQL语法。

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

SQL小提琴-

第二个变量,其中数据按类型排序(
id
使用
@id
变量生成)

SQL小提琴-

如果要将其放入具有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

带有
类型1和类型2的案例

测试数据

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)
);
质疑

结果

SELECT *
FROM SumTable

SQL FIDLE-

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

SQL小提琴-

第二个变量,其中数据按类型排序(
id
使用
@id
变量生成)

SQL小提琴-

如果要将其放入具有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

带有
类型1和类型2的案例

测试数据

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)
);
质疑

结果

SELECT *
FROM SumTable
SQL小提琴-

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

请参阅: SQL小提琴-

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

请参阅:
SQL FIDLE-

您可以使用连接来完成此操作:

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:

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

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:

用您尝试过的代码更新您的问题以解决此问题。用您尝试过的代码更新您的问题以解决此问题。根据要求的输出,输出中应有5行空值不匹配,而在小提琴中,在
Mar
表中只有4行scheck
id
,如@Laxmi sad-row
(4,51,a005)
。可能正确的数据是
(5,51,a005)
。我认为这个查询是正确的。id是自动递增的,我认为用户应该是基于类型的数据列值尝试第二个变量。我已经更新了我的答案。SQL FIDLE-@Laxmi-如果您指的是第一个查询,那么它是不正确的,因为我第一次误解了作者的预期结果。他希望按
类型对数据进行排序
,并为其生成新的
id
。它不是源表(1月、2月、3月)中的
IDs
。根据要求的输出,输出中应有5行空值不匹配,而在小提琴中,它只在
Mar
表中给出4行scheck
id
,如@Laxmi sad-row
(4,51,a005)
。可能正确的数据是
(5,51,a005)
。我认为这个查询是正确的。id是自动递增的,我认为用户应该是基于类型的数据列值尝试第二个变量。我已经更新了我的答案。SQL FIDLE-@Laxmi-如果您指的是第一个查询,那么它是不正确的,因为我第一次误解了作者的预期结果。他希望按
类型对数据进行排序
,并为其生成新的
id
。它不是源表(一月、二月、三月)中的
IDs
。id是自动递增的,我认为用户应该是基于类型列的数据value@AnkitAgrawal也许是的,我也试过和你一样的答案。但是上面给出的输出有点混乱。对不起,我需要的是新表中的'id'是自动递增的,而不是来自上面三个。id是自动递增的,我认为用户应该是基于类型列的数据value@AnkitAgrawal也许是的,我也试过和你一样的答案。但是上面给出的输出有点混乱。抱歉,但我需要新表中的“id”是自动递增的,而不是来自上面三个。抱歉,但我需要新表中的“id”是自动递增的,而不是来自上面三个。抱歉,但我需要新表中的“id”是自动递增的,而不是来自上面三个。
SELECT *
FROM SumTable
-------------------------------------------------------------------------
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
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