简体   繁体   中英

SQL: Insert Into with multiple rows

Let's say I have two tables:

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

How can I put the first 5 people from Person who has hasFamily='no' into Family with idFamily=1? I've already figured out that I can use:

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

But I'm unsure about the insert part.

(I've already asked this before on http://www.reddit.com/r/learnprogramming/comments/2sv1n4/sql_question_about_insert_into/ but the solution did not work)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM