Home > Database > Mysql Tutorial > How to Insert Data from One SQL Table to Another Using a Single Statement?

How to Insert Data from One SQL Table to Another Using a Single Statement?

Susan Sarandon
Release: 2025-01-04 10:21:34
Original
419 people have browsed it

How to Insert Data from One SQL Table to Another Using a Single Statement?

Inserting Data from One Table into Another Using SQL

You have a requirement to import data from a "raw data" table into a "processed data" table. Specifically, you wish to perform this operation using only SQL, without the need for intermediary code or scripts.

Solution

To achieve your goal, you can employ the following SQL statement:

INSERT INTO action_2_members (campaign_id, mobile, vote, vote_date)
SELECT campaign_id, from_number, received_msg, date_received
FROM `received_txts`
WHERE `campaign_id` = '8'
Copy after login

This statement will extract data from the received_txts table, filtering the results based on the specified campaign_id, and directly insert the selected rows into the action_2_members table.

In your specific example, the query q1 would not be necessary as the data retrieval can be done within the INSERT statement. This optimized approach ensures that the data is fetched and inserted in a single operation, potentially improving performance and efficiency.

The above is the detailed content of How to Insert Data from One SQL Table to Another Using a Single Statement?. 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