1

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

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

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

使用 MySQL 数据库

4

3 回答 3

2
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'
于 2013-08-15T19:44:41.577 回答
0

应该是这样的

INSERT INTO table (age, p_id)
SELECT '22', people.id
    FROM people
    WHERE people.name = 'bob
于 2013-08-15T19:46:23.083 回答
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'
于 2013-08-15T19:46:48.070 回答