0

I have a three tables. enter image description here

Alan1, Alan2, Alan3 are the same in all tables.I want to merge tables. The difference of the tables is the rightmost column. How should I write a SQL Query?

1
  • 1
    Use JOIN to combine rows with common column values. Commented Aug 6, 2020 at 23:03

1 Answer 1

1

You can join. As commented by Barmar, the idea is to use the first 3 columns as join keys;

select a.*, b.alan4 as alan4b, c.alan4 as alan4c
from a
inner join b
    on  b.alan1 = a.alan1
    and b.alan2 = a.alan2
    and b.alan3 = a.alan3
inner join c
    on  c.alan1 = a.alan1
    and c.alan2 = a.alan2
    and c.alan3 = a.alan3

This gives you rows that are available in all 3 tables. Say you want to allow "missing" rows in b and/or c, then you need to change the two inner joins to left joins.

Sign up to request clarification or add additional context in comments.

Comments

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.