SQL:使用多行插入到

时间:2015-01-19 07:50:25

标签: mysql sql insert

假设我有两张桌子:

Family [idFamily, idPerson] and
Person [idPerson, hasFamily]

如何将拥有hasFamily ='no'的人中的前5个人放入idFamily = 1的家庭?我已经发现我可以使用:

select * from Person where hasFamily = 'no' limit 5    

但我不确定插入部分。

(我之前已在http://www.reddit.com/r/learnprogramming/comments/2sv1n4/sql_question_about_insert_into/上问过此问题,但解决方案无效)

1 个答案:

答案 0 :(得分:1)

insert into family (idFamily, idPerson)
select 1, idPerson 
from Person 
where hasFamily = 'no' 
limit 5