I have a "Groups" table and a "Participants" table. Now I need to insert one Participant for each Group. How would I automate this?
INSERT INTO "Participants" ("Name", "FirstName", "GroupID") VALUES ("GENERIC", "GENERIC", GroupID)
This Insert should be called for each Group in the Groups table, and the "GroupID" replaced with the corresponding ID.
Would this work with a subquery?
Thanks, martin
You can use subqueries in SELECT, INSERT, UPDATE, and DELETE statements wherever expressions are allowed. For instance, you can use a subquery as one of the column expressions in a SELECT list or as a table expression in the FROM clause. A DML statement that includes a subquery is referred to as the outer query.
a subquery must be enclosed in the parenthesis.
When you put a select statement into a FROM clause, it becomes a subquery. The subquery returns a temporary table in database server's memory and then it is used by the outer query for further processing.
INSERT INTO SELECT...
INSERT INTO
Participants
(
Name,
FirstName,
GroupID
)
SELECT
'GENERIC',
'GENERIC',
GroupID
FROM
Groups
INSERT INTO `Participants`
SELECT ("Name", "FirstName", `group_id`)
FROM `Group`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With