Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql - Inserting multiple rows with a joined subquery?

This query will return a list of project IDs that represent forum threads:

SELECT id FROM `proj_objects` WHERE  type='fthread';

This query will subscribe a user (whose ID in the users table is '37') to the forum thread with an ID of '122':

INSERT INTO `subscrips` VALUES ( 37, 122 ) ;

I'd like to insert multiple rows that will subscribe user 37 to all project objects where type is fthread. Can I do this in a single query?

like image 386
user420113 Avatar asked Aug 14 '10 00:08

user420113


People also ask

Can subquery return multiple rows?

Multiple-row subqueries are nested queries that can return more than one row of results to the parent query. Multiple-row subqueries are used most commonly in WHERE and HAVING clauses. Since it returns multiple rows, it must be handled by set comparison operators (IN, ALL, ANY).

Can we insert multiple rows single insert statement?

INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

Which operator is used when the subquery returns multiple rows?

Answer: D. Multiple-row subqueries return more than one row of results. Operators that can be used with multiple-row subqueries include IN, ALL, ANY, and EXISTS.

How can I add multiple values in one column in MySQL?

MySQL INSERT multiple rows statement In this syntax: First, specify the name of table that you want to insert after the INSERT INTO keywords. Second, specify a comma-separated column list inside parentheses after the table name. Third, specify a comma-separated list of row data in the VALUES clause.


1 Answers

Use:

INSERT INTO `subscrips` 
SELECT 37, id 
  FROM `proj_objects` 
 WHERE type = 'fthread'
like image 112
OMG Ponies Avatar answered Sep 16 '22 16:09

OMG Ponies