MySql带有select的多个插入

时间:2014-07-17 05:13:33

标签: mysql

我正在尝试使用select插入多行,如下所示:

INSERT INTO Check(
name, price, 
count, order_id) 
SELECT 
p.name, p.price, 
:count, :order 
FROM Product p 
WHERE p.id 
IN(:id1, :id2, :id3)

问题是每个:id1, :id2, ...都拥有:coun1, :count2, :count3

2 个答案:

答案 0 :(得分:1)

你在寻找这样的东西吗?:

INSERT INTO Check(
name, price, 
count, order_id) 
SELECT 
p.name, p.price, 
IF (id = :id1, :count1, IF (id = :id2, :count2, :count3)), :order 
FROM Product p 
WHERE p.id 
IN(:id1, :id2, :id3)

答案 1 :(得分:1)

另一种选择是..创建一个单独的表,其中包含那些变量id,并计算如

create table tab1(id int, count int);

insert into tab1(id,count) values(:id1, :coun1);

然后你可以试试

INSERT INTO `Check`(
name, price, 
count, order_id) 
SELECT 
p.name, p.price, t.id, t.count 
FROM Product p
JOIN tab1 t on p.id = t.id