简体   繁体   中英

Mysql select from a table and insert into another

I have a mysql table called jos_users_quizzes with the following columns:

id
quiz_id
user_id

I have a second table called  jos_users with this columns


id 
name
username
department

the user_id on first table is linked with the id of second table so quiz_id = id (jos_users)

How can build a query to multiple insert the ids of a selected department into the jos_users_quizzes table... in one click

I am thinking meabe a sub query or a loop, but no sure how.

Thanks in advance!

INSERT jos_users_quizzes (quiz_id, user_id)
SELECT $quizID, id
FROM jos_users
WHERE department = 'selected department'

With INSERT ... SELECT , you can quickly insert many rows into a table from one or many tables. For example

INSERT INTO jos_users_quizzes (quiz_id)
  SELECT jos_users.id
  FROM jos_users WHERE jos_users.department = 100;

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