在MySQL中使用INSERT ... SELECT添加额外的列值 [英] Adding an extra column value with INSERT ... SELECT in MySQL
本文介绍了在MySQL中使用INSERT ... SELECT添加额外的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表,我的目标是将特定数据从第一个表移至第二个表,以及移动该数据的原因.例如:
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:
raw_data_table
SELECT * FROM raw_data_table where id IS NULL;
我想将其移到第二个表中,除了一个额外的列reason
外,该表与第一个表相同.我试过了:
I would want to move this into the second table, which is identical to the first table except for an extra column reason
. I tried:
INSERT INTO bad_data_table
(SELECT * FROM raw_data_table WHERE id IS NULL), "The ID is NULL";
但是这将返回语法错误.如何复制整行并添加原因值?
But this returns a syntax error. How can I copy the entire row over and add the reason value?
推荐答案
INSERT INTO bad_data_table
SELECT *, 'The ID is NULL' AS Reason
FROM raw_data_table
WHERE id IS NULL;
这篇关于在MySQL中使用INSERT ... SELECT添加额外的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文