INSERT中的SQL子查询?

时间:2013-08-15 19:42:13

标签: mysql sql insert subquery

是否可以在INSERT语句中使用子查询?

INSERT INTO table (age, p_id) 
VALUES('22', '(SELECT id FROM people WHERE name='Bob')')

我正在尝试的工作形式吗?

使用MySQL数据库

3 个答案:

答案 0 :(得分:1)

INSERT INTO MyTable (age, p_id)
SELECT '22', ( select id from people where name = 'bob')

或者你可以这样做

INSERT INTO MyTable (age, p_id)
SELECT '22', id 
FROM people
WHERE name = 'bob'

答案 1 :(得分:0)

应该是

INSERT INTO table (age, p_id)
SELECT '22', people.id
FROM people
WHERE people.name = 'bob

答案 2 :(得分:0)

INSERT INTO table (age, p_id) 
SELECT '22', (SELECT id FROM people WHERE name='Bob'))

或更好......

INSERT INTO table (age, p_id) 
SELECT '22',id FROM people WHERE name='Bob'