简体   繁体   English

使用哪种类型的JOIN

[英]What type of JOIN to use

What type of JOIN would I use to get table1 and table2 to be matched only once. 我将使用哪种类型的JOIN来使table1table2仅匹配一次。 For example, I have table1 (40 rows) and table2 (10000 rows). 例如,我有table1 (40行)和table2 (10000行)。 But I get table1 repeated over and over when I use a join on table1.LocationArea = table2.Location 但是当我在table1.LocationArea = table2.Location上使用table1.LocationArea = table2.Location时,会一遍又一遍地重复table1

What I get:                         What I wish I could get:
t1.LocationArea,t2.Location         t1.LocationArea,t2.Location
---------------------------         ---------------------------
az,az                               az,az
az,az                               null,az
ca,ca                               ca,ca
il,il                               il,il
tx,tx                               tx,tx
tx,tx                               null,tx
az,az                               null,az
                                    null,il
                                    null,ca

I wish to end up with 10000 records in the query. 我希望在查询中得到10000条记录。

I have tried inner join , left , and I am using ZOHO reports which does not support outer join's. 我尝试了inner joinleft ,并且我正在使用不支持外部联接的ZOHO报告。

SELECT "table1"."LocationArea", "Location" 
FROM "table2"
left join "table1" on  "Location" = "table1"."LocationArea"

Obviously, you have duplicate values for both of the joining columns. 显然,两个连接列都有重复的值。 Instead of the Cartesian product an [INNER] JOIN would produce for this, you want each row to be used only once . 而不是会为此生成[INNER] JOIN笛卡尔乘积 ,您希望每行仅使用一次 You can achieve this by adding a row number ( rn ) per duplicate and join on rn additionally. 您可以通过为每个重复项添加行号( rn )并附加加入rn来实现此目的。

Each table can have more or fewer dupes for the same value than the other unless you have additional restrictions in place (like a FK constraint) - but there is nothing in your question. 除非您有其他限制(例如FK约束),否则每个表可以具有比另一个表更多或更少的重复值,除非您没有其他问题。 To keep all rows one would use a FULL [OUTER] JOIN . 要保留所有行,请使用FULL [OUTER] JOIN But you want to keep 10000 records in the result, which is the cardinality of table2 . 但是您想要在结果中保留10000条记录,这就是table2的基数。 So it must be a LEFT [OUTER] JOIN on table1 (with 40 rows) - and exclude possible excessive rows from table1 . 因此,它必须是table1上的LEFT [OUTER] JOIN (具有40行),并从table1排除可能过多的行。

SELECT t1."LocationArea", t2."Location"
FROM  (
   SELECT "Location"
        , row_number() OVER (PARTITION BY "Location") AS rn
   FROM   table2
   ) t2
LEFT JOIN (
   SELECT "LocationArea"
        , row_number() OVER (PARTITION BY "LocationArea") AS rn
   FROM   table1
   ) t1 ON t1."LocationArea" = t2."Location"
       AND t1.rn = t2.rn;

Works for Postgres or SQL Server. 适用于Postgres或SQL Server。 MySQL doesn't support window functions, you would need a substitute: MySQL不支持窗口函数,您需要一个替代方法:

To be clear: LEFT JOIN is just shorthand for LEFT OUTER JOIN , so you are already using an outer join. 需要说明的是: LEFT JOIN只是LEFT OUTER JOIN简写,因此您已经在使用外部LEFT OUTER JOIN Your statement is a misunderstanding : 您的陈述是一种误解

I am using ZOHO reports which does not support outer join's. 我正在使用不支持外部联接的ZOHO报告。

SELECT * FROM table1 LEFT JOIN table2 ON `table_1_primary_key` = `table_2_primary_key`

Or SELECT colname FROM table1 LEFT JOIN table2 ON table_1.colname = table_2.colname 或SELECT colname FROM table1 LEFT JOIN table2 ON table_1.colname = table_2.colname

Depending on the structure of your database 取决于您的数据库结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM