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

前端 未结 5 1521
情话喂你
情话喂你 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条回答
  •  面向向阳花
    2021-01-30 12:52

    As simple as this :

    INSERT INTO config (id, customer_id, domain) 
    SELECT DISTINCT id, customer_id, domain FROM config;
    

    If you want "www.example.com" as the domain, you can do :

    INSERT INTO config (id, customer_id, domain) 
    SELECT DISTINCT id, customer_id, 'www.example.com' FROM config;
    

提交回复
热议问题