[英]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
.我正在处理一个包含名为
ios_app_id
、 android_app_id
和app_id
的字段的查询。 The app_id
from downloads table can be either ios_app_id
or android_app_id
in the products table. downloads表中的
app_id
可以是products表中的ios_app_id
或android_app_id
。
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
?是否正确,因为我不能只在
p.ios_app_id = d.app_id
上运行下载和产品表的简单连接,然后在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
谢谢
Just join
twice:只需
join
两次:
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
.这应该比您使用
union
的方法更有效。 Basically, it attempts joining using the two ids.基本上,它尝试使用两个 ID 加入。 The
coalesce()
combines the results into a single column. coalesce()
将结果组合到单个列中。
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.请记住,
INNER JOIN
的目的是获取存在于两个数据集(我们称它们为表 A 和表 B)上的值,并使用特定的列来连接它们。 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
.在您的示例中,如果您尝试执行两次
INNER JOIN
,会发生什么情况是您第一次执行INNER JOIN
时,完整的 PRODUCTS 表是您的表 A,并且您获得了所有已下载 ios_app 的产品,但是现在(这是关键部分)这个结果成为你的新数据集,所以它成为你的新表 A 用于下一个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.这就是您需要使用
UNION
的原因,因为您需要独立获取结果然后添加它们。
An alternative would be to use LEFT JOIN
, but you could get null values and duplicates -and its not too "clean"-.另一种方法是使用
LEFT JOIN
,但您可以获得 null 值和重复值 - 而且它不太“干净” -。 So, for your particular case, I think using UNION
is much clearer and easier to understand.因此,对于您的特定情况,我认为使用
UNION
更清晰,更容易理解。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.