是否可以在INSERT语句中使用子查询?
INSERT INTO table (age, p_id)
VALUES('22', '(SELECT id FROM people WHERE name='Bob')')
我正在尝试的工作形式吗?
使用MySQL数据库
答案 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'