0

如果我想同时找到一个主键并将其用作插入语句中的外键,那么我可以使用下面的查询(在这个问题上找到它)

INSERT INTO test (id, value) 
SELECT foobar.id, 20
FROM foobar WHERE name = 'joe';

但是如果我想插入多个外键怎么办?声明会如何?

INSERT INTO test (id1, id2, id3, value) 
SELECT foobar1.id, foobar2.id, foobar3.id, 20
FROM foobar1 WHERE name = 'joe'
FROM foobar2 WHERE name = 'joe'
FROM foobar3 WHERE name = 'joe';

谢谢

4

2 回答 2

2

这行得通吗?:

INSERT INTO test ( id1, id2, id3, value ) 
SELECT 
foobar1.id, foobar2.id, foobar3.id, 20
FROM 
foobar1, foobar2, foobar3
WHERE 
foobar1.name = 'joe'
AND 
foobar2.name = 'joe'
AND 
foobar2.name = 'joe';
于 2012-05-25T19:38:43.493 回答
1

我想这就是你想要的:

INSERT INTO test (id1, id2, id3, value) 
SELECT foobar1.id, foobar2.id, foobar3.id, 20
FROM foobar1,foobar2, foobar3
WHERE foobar1.name = 'joe'
AND foobar2.name = 'joe'
AND foobar3.name = 'joe';
于 2012-05-25T19:37:58.267 回答