Home > Database > Mysql Tutorial > How to Insert Multiple Rows from a SELECT Subquery in MySQL?

How to Insert Multiple Rows from a SELECT Subquery in MySQL?

Mary-Kate Olsen
Release: 2024-10-29 21:36:30
Original
348 people have browsed it

How to Insert Multiple Rows from a SELECT Subquery in MySQL?

Inserting Multiple Rows from a SELECT Query into a New Table

When attempting to insert data into a new table using a SELECT subquery that returns multiple rows, MySQL may encounter an error: "Subquery returns more than 1 row". To resolve this issue, consider the following approach:

Solution:

Modify your INSERT statement to combine the static values with the SELECT query. For instance, to populate the Results table with all returning rows:

INSERT INTO Results (People, names)
SELECT d.id, "Henry"
FROM Names f
JOIN People d ON d.id = f.id
Copy after login

By incorporating the static value "Henry" within the SELECT query, MySQL can successfully insert the results into the Results table. This method allows you to insert multiple rows even if the subquery returns an indeterminate number of rows.

The above is the detailed content of How to Insert Multiple Rows from a SELECT Subquery in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template