2

I've looked up ways to solve my problem here on SO and other sources, but nothing i have tried worked, so here i am.

I need to concatenate two tables with different columns, as it follows:

(data is just a represetantion, not my actual data and i'm using Postgres SQL)

Table_1:

name   price  id  location
test   1.0     7  Canada

Table_2

name     store         sale  price  location
testing  local_store   54    2.0      US

My actual tables have over 30 rows each, but the resulting table i need would look like this:

Table_concat:

name     price  store        sale   id    location   
test     1.0    null         null   7     Canada  (row from Table_1)
testing  2.0    local_store  54     null  US      (row from Table_2)

Basically, i need to put one table on top of another and when the columns do not match, a null value should apear. Can anyone help me? I can provide further explanation if necessary.

1
  • 1
    You want UNION ALL. Select null for the non-matching columns. Commented Mar 31, 2021 at 12:56

1 Answer 1

3

Try this query:

SELECT 
  name, price, null AS store, null AS sale, id, location
FROM Table_1

UNION 

SELECT 
  name, price, store, sale, null AS id, location
FROM Table_2
Sign up to request clarification or add additional context in comments.

1 Comment

The combination of null values with UNION worked just fine, thank you!

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.