Adding an extra column value with INSERT … SELECT in MySQL

前端 未结 2 479
醉酒成梦
醉酒成梦 2020-12-05 15:53

I have two tables and my goal is to move specific data from the first table into the second table along with a reason for why that data was moved. For instance:



        
相关标签:
2条回答
  • 2020-12-05 16:18

    Try this query:

    INSERT INTO bad_data_table VALUES (
    (SELECT * FROM raw_data_table WHERE id IS NULL LIMIT 1), "The ID is NULL");
    

    The subquery here needs to have 1 row!

    0 讨论(0)
  • 2020-12-05 16:25
    INSERT INTO bad_data_table 
    SELECT *, 'The ID is NULL' AS Reason
    FROM raw_data_table
    WHERE id IS NULL;
    
    0 讨论(0)
提交回复
热议问题