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'
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!