MySQL使用一个select插入多行

时间:2017-01-02 15:11:59

标签: mysql select rows

我多次运行以下查询:

insert ignore into temp_table (ID, Price, Stock, Vendor)
select ID, Price_Vendor_A, Stock_Vendor_A, 'Vendor_A' from temp_table2 limit 1;
insert ignore into temp_table (ID, Price, Stock, Vendor)
select ID, Price_Vendor_B, Stock_Vendor_B, 'Vendor_B' from temp_table2 limit 1;

2个选择查询将指向同一行。

有更快的方法吗?

2 个答案:

答案 0 :(得分:0)

我不知道这会更快,但您可以尝试插入一个select * from table1 a LEFT OUTER JOIN ( select * from table99 where col = 1 )b ON (a.col1 = b.col1) WHERE a.col2 = b.col2 AND SIGN(a.col3) = 1 LEFT OUTER JOIN ( select * from table99 where col = 2 )c ON (a.col1 = c.col1) WHERE a.col2 = c.col2 AND SIGN(a.col3) = 1; 查询:

UNION

答案 1 :(得分:0)

以下将是最快的方式。消除多个选择。

 SELECT ID, Price_Vendor_A, Stock_Vendor_A,Price_Vendor_B, Stock_Vendor_B into @ID, @Price_Vendor_A,@Stock_Vendor_A,@Price_Vendor_B,@Stock_Vendor_B FROM temp_table2 LIMIT 1;

insert ignore into temp_table (ID, Price, Stock, Vendor) SELECT @ID as ID, @Price_Vendor_A as price, @Stock_Vendor_A as stock ,"Vendor_A" AS Vendor UNION ALL  SELECT @ID as ID, @Price_Vendor_B as price, @Stock_Vendor_B as stock ,"Vendor_B" AS Vendor