15

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;

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?

2 Answers 2

33
INSERT INTO bad_data_table 
SELECT *, 'The ID is NULL' AS Reason
FROM raw_data_table
WHERE id IS NULL;
Sign up to request clarification or add additional context in comments.

1 Comment

I would look at breaking that * into column names as well. Otherwise, when someone ads a new column to the Reason table things will start blowing up.
0

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!

1 Comment

Error - #1241 - Operand should contain 1 column(s)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.