Chris90
Chris90

Reputation: 1998

Joining two different columns from one table to the same column in a different table?

I am working on a query that has fields called ios_app_id, android_app_id and app_id. The app_id from downloads table can be either ios_app_id or android_app_id in the products table.

Is it correct that because of that I cannot just run a simple join of downloads and products table on on p.ios_app_id = d.app_id and then join again on on p.android_app_id = d.app_id? Would that cause an incorrect number of records?

select p.product_id, d.date, d.downloads,
from products p 
inner join download d
on p.ios_app_id = d.app_id

UNION 

select p.product_id, d.date, d.downloads
from products p
inner join download d
on p.android_app_id = d.app_id

I would try:

 select p.product_id, d.date, d.downloads,
    from products p 
    inner join downloads d
    on p.ios_app_id = d.app_id
    inner join downloads d
    on p.android_app_id = d.app_id

Basically I am trying to understand why the union here is needed instead of just joining the two fields twice? Thank you

Upvotes: 3

Views: 2374

Answers (3)

RonCG
RonCG

Reputation: 369

Remember that the purpose of an INNER JOIN is to get the values that exists on BOTH sets of data (lets called them table A and table B), using a specific column to join them. In your example, if you try to do the INNER JOIN twice, what would happen is that the first time you execute the INNER JOIN, the complete PRODUCTS table is your table A, and you obtain all the products that have downloaded the ios_app, but now (and this is the key part) this result becomes your new dataset, so it becomes your new table A for the next inner join. And thats the issue, cause what you would want is to join the whole table again, not just the result of the first join, but thats not how it works. This is why you need to use the UNION, cause you need to obtain your results independently and then add them.

An alternative would be to use LEFT JOIN, but you could get null values and duplicates -and its not too "clean"-. So, for your particular case, I think using UNION is much clearer and easier to understand.

Upvotes: 1

If you do left join in first query it will work.

create table all_products as (select p.product_id, d.date, d.downloads,
from products p 
left join downloads d
on p.ios_app_id = d.app_id)
select a.product_id, d.date, d.downloads from all_products a left join downloads d 
on a.android_app_id = d.app_id inner join 

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1271151

Just join twice:

select p.product_id,
       coalesce(di.date, da.date),
       coalesce(di.downloads, da.downloads)
from products p left join
     downloads di
     on p.ios_app_id = di.app_id left join
     downloads da
     on p.android_app_id = da.app_id;

This should be more efficient than your method with union. Basically, it attempts joining using the two ids. The coalesce() combines the results into a single column.

Upvotes: 2

Related Questions