-1

I have 2 tables, table one contains phone numbers and owner name, and table two have phone , name and also its have city and street. the problem in table two is that the owner name is the name of the one who pays for the phone number and not the actual owner (for example if my dad pays for my phone number, his name will appear along side my number). what i want to do is to write a query that checks if the phone number exists on both tables and if it does, i want to see the owner name from table one and the other info from table two.

also if there is a number that exists only on table two i want to see it to. and if the number is only on table one i want to see it also but with the address and city column empty.

example for expected result:

enter image description here

i have no idea how to do this, it will be very helpful if someone could tell me where to look for answer.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
davidla
  • 85
  • 1
  • 11

1 Answers1

0

You want a full join. A simple method to implement this with SQLite is to use union all:

select t2.address, t2.city, t1.phone, t1.name
from table1 t1 left join
     table2 t2
     using (phone)
union all
select t2.address, t2.city, t2.phone, t2.name
from table2 t2
where not exists (select 1 from table1 t1 where t1.phone = t2.phone);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786