Moji
Moji

Reputation: 11

SQL insert into and select syntax

Using MySQL, I want to 'insert into' a table with specific values which must select between IDs that are equal to an IDs from another table.

I need to do something similar to this :

insert into activity (date_created, action, source) values ("2018-12-05 07:00:00", "UNSUBSCRIBE", "MIGRATE") where activity.customer_id = (select customer.id from customer where customer.cell_phone_number = "123456");

Upvotes: 0

Views: 29

Answers (1)

Emmanuel Rosa
Emmanuel Rosa

Reputation: 9895

You can use the INSERT...SELECT syntax:

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    SELECT ...
    [ON DUPLICATE KEY UPDATE assignment_list]

Perhaps something like this:

INSERT INTO activity (date_created, action, source, customer_id) 
SELECT "2018-12-05 07:00:00" AS date_created, "UNSUBSCRIBE" AS action, "MIGRATE" AS source, id
FROM customer
WHERE cell_phone_number = "123456";

Upvotes: 1

Related Questions