2
votes

I want to copy data to column A in Table1 from column B in Table2. Rows for column A are empty and there are exists other columns in Table1 with already populated data. So I need to grab the whole column B from Table2 and insert all those values in column A in Table1. The two table are completely identical, except that column A has no values at all.

How do I do this in sqlite3?

3

3 Answers

5
votes

Use:

INSERT INTO TABLE1
SELECT B,
       NULL,
       NULL,
       NULL
  FROM TABLE2

Use NULL as the placeholder for however many columns you can't populate from TABLE2, assuming TABLE1 columns allow NULL values.

1
votes
UPDATE TABLE1 SET A = (SELECT B FROM TABLE2 WHERE ...)

Come to think of it, if the tables are truly identical, why do you need two of them? In any case you can also do this:

BEGIN;
DELETE FROM TABLE1;
INSERT INTO TABLE1 (A, col1, col2, ...) SELECT (B, col2, col2, ...) FROM TABLE2;
COMMIT;
0
votes

Try this: INSERT INTO TABLE1 (A) SELECT B FROM TABLE2