Home > OS >  Merge two tables by overlap?
Merge two tables by overlap?

Time:04-07

I'm new to SQL and I'm looking for a real query that kinda does this:

SELECT * FROM 'Table_A' AND 'Table_B'
WHERE 'Table_A.Country' matches 'Table_B.Land' 
AND WHERE 'Table_A.CITY' matches 'Table_B.Town'
AND WHERE 'Table_A.Language' matches 'Table_B.Spoken'

So I get the result as in the picture below.

enter image description here

CodePudding user response:

You want a simple join with an on clause to map between the different column names:

select *
from Table_A a
join Table_B b on a.Country = b.Land and a.City = b.Town and A.Language = B.Spoken;

If it's an option change the schema so the same column name is used for the thing across the two tables. Also look into normalizing the data to avoid the duplication (Town probably implies everything else so that would violate 2nd normal form).

CodePudding user response:

As there is no any foreign key defined, so you can use :

select *from Table_A as A
join Table_B as B on A.Country = B.Land and A.City = B.Town and A.Language = B.Spoken;
  • Related