简体   繁体   English

将一个表中的两个不同列连接到另一个表中的同一列?

[英]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_idandroid_app_idapp_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_idandroid_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.

相关问题 SQL:通过联接将一个表中的列链接到另一表中的不同列 - SQL: link a column in one table to different columns in another table with joining 从一个表中的两个不同列中选择数据,这些列指向另一个表中的同一列 - selecting data from two different columns in one table that point to the same column in another table 将同一表中的列中的不同值连接到一行中 - Joining different values in columns, within the same table, into one row 使用来自同一源的两个不同列创建一个表/视图 - Create one table/view with two different columns from the same source 按条件从一列到不同表的两列的外键 - foreign key from one column to two columns of different table by condition 单个查询根据不同的条件从同一个表中将一列的数据拆分为两列 [SQL] - Single query to split out data of one column, into two columns, from the same table based on different criteria [SQL] 将来自不同表的两列合并为一列 - Joining two columns into one column from different tables 连接具有不同条件的同一张表的列-SQL - joining columns of same table with different criteria - SQL 从不同的列加入同一个表上的多个计数? - Joining multiple counts on the same table, from different columns? sql加入同一个表但不同的列 - sql Joining on same table but different column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM