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 Answer
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.

JOINto combine rows with common column values.