2

I am a little confused about merging one table into another. My two tables looks like so:

Table A                                         Table B
id | name | likes | email | username            id | name | email   | username
1  | joe  |  3    |  null | null                1  | ben  | [email protected] | user


Result: Table A
id | name | likes | email   | username
1  | joe  | 3     | null    | null
2  | ben  | null  | [email protected] | user

My issue is that I do not want to overwrite the properties that are in the Table A. Is this a simple UNION?

1 Answer 1

3

You can just execute INSERT INTO..SELECT statement,

INSERT INTO TableA(id, name, likes, email, username)
SELECT id, name, NULL AS likes, email, username
FROM TableB

What it does is it copies all records from TableB into TableA. But if you want the records for projection only, then a simple UNION will do just fine

SELECT id, name, likes, email, username FROM tableA
UNION
SELECT id, name, NULL AS likes, email, username FROM TableB
Sign up to request clarification or add additional context in comments.

5 Comments

Okay that looks like what I was trying to write originally but what is going on with the SELECT id, name NULL AS ...?
it sets NULL value for LIKES. You can alternatively do this, INSERT INTO TableA(id, name, email, username) SELECT id, name, email, username FROM TableB
Okay so whatever values I do not want to insert I can leave them out of the columns of the initial INSERT INTO statement?
yes, if you leave them out, the values for that column will be set as NULL :D
Thanks for your help and quick response!

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.