MySQL: How to insert a record for each result in a SQL query?

前端 未结 5 1508
情话喂你
情话喂你 2021-01-30 12:36

Say I have a select

SELECT DISTINCT id, customer_id, domain FROM config WHERE type = \'foo\';

which returns some records.

How can I do

5条回答
  •  梦毁少年i
    2021-01-30 13:07

    EDIT- After reading comment on @Krtek's answer.

    I guess you are asking for an update instead of insert -

    update config set domain = 'www.example.com'
    

    This will update all existing records in config table with domain as 'www.example.com' without creating any duplicate entries.

    OLD ANSWER -

    you can use something like -

    INSERT INTO config (id, customer_id, domain)
    select id, customer_id, domain FROM config
    

    Note:- This will not work if you have id as primary key

提交回复
热议问题