Home > Database > Mysql Tutorial > How to Insert Data from One SQL Table to Another Based on Campaign ID?

How to Insert Data from One SQL Table to Another Based on Campaign ID?

Susan Sarandon
Release: 2025-01-05 07:51:38
Original
697 people have browsed it

How to Insert Data from One SQL Table to Another Based on Campaign ID?

Inserting Data from One Table into Another Using SQL

You wish to insert data from the "received_txts" table into the "action_2_members" table based on a specific campaign ID. The data to be inserted includes campaign ID, mobile number, received message, and date received.

SQL Query for Data Insertion

To achieve this, you can utilize the following SQL query:

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

Explanation

  • The INSERT statement specifies the target table "action_2_members" and the columns to be inserted into.
  • The SELECT statement retrieves data from the "received_txts" table based on the given campaign ID.
  • The WHERE clause ensures that only rows with the matching campaign ID are included in the selection.
  • The data retrieved by the SELECT statement is then inserted into the target table, matching the columns in both tables.

Note:

  • The query assumes that both tables have matching data types for the columns being inserted.
  • The performance of the query may vary depending on the number of rows in the "received_txts" table. If there are a significant number of rows, consider optimizing the query using techniques such as indexing.

The above is the detailed content of How to Insert Data from One SQL Table to Another Based on Campaign ID?. 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