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