简体   繁体   English

除非为空,否则如何基于一列联接两个表,在这种情况下,应基于两个不同的列联接

[英]How do you join two tables based on one column unless it's null, in which case join based on two different columns

So this is confusing, but lets start with the following tables: 因此,这令人困惑,但让我们从下表开始:

 Table1           Table2
 ID1  |  ID2  ||  ID1  |  ID2
 -----------  ||  -----------
 jve  |  234  ||  jve  |  null
 null |  234  ||  dav  |  234
 rev  |  584  ||  rev  |  null
 null |  584  ||  ewj  |  584
 avv  |  442  ||  avv  |  null
 null |  442  ||  pol  |  442

*The tables are organized for ease of understanding but aren't in reality *表格是为了便于理解而组织的,但实际上并非如此

What I'm trying to do is join Table1 and Table2 on T1.ID1 = T2.ID1. 我想做的是在T1.ID1 = T2.ID1上加入Table1和Table2。 However, if ID1 happens to be null, then I want to join them on T1.ID2 = T2.ID2 and not the first way. 但是,如果ID1恰好为空,那么我想在T1.ID2 = T2.ID2上加入它们,而不是第一种方法。

What I've gotten up to with googling is 我对谷歌搜索的了解是

SELECT *
  FROM table1 t1
       INNER JOIN table2 t2
          ON (    t1.ID1 = COALESCE (t2.ID1, t1.ID1)
              AND t1.ID2 = COALESCE (t2.ID2, t1.ID2))
 WHERE ID2 = '234';

The results I'm trying to get to pop up are just to horizontally merge the above tables as such: 我要弹出的结果只是将上述表格横向合并为:

Results
-------
jve  | 234 | jve | null
null | 234 | dav | 234
etc..

The reason for this is that there are many more columns beyond ID2 that I'm trying to align between table 1 and table 2. 这样做的原因是,我试图在ID1以外的表1和表2之间对齐的列还有更多。

The main struggle here is I'm not sure how to put the logic, 这里主要的困难是我不确定如何表达逻辑,

if( T1.ID1 == null )
{
   ON T1.ID2 = T2.ID2
}
else
{
   ON T1.ID1 = T2.ID1
}

into SQL 进入SQL

If more information is needed, I will gladly add to my post. 如果需要更多信息,我会很乐意添加到我的帖子中。

I forgot to include that if it joins the first way, it shouldn't join the second way afterwards. 我忘了包括,如果它以第一种方式加入,那以后就不应该以第二种方式加入。 What I mean by this, is that first row, jve | 我的意思是第一行 234 should join to jve | 234应该加入jve | null and then the next row, null | null,然后下一行,null | 234 should be grabbed instead of joining jve | 应该抓住234,而不是加入jve。 234 to dav | 234至dav | 234... if that made any sense.. 234 ...如果有任何道理..

I need these two tables to join using one method or the other, but never both. 我需要使用一个方法或另一个方法来连接这两个表,但绝不能同时使用这两个表。

Your logic sounds like an OR condition in the ON clause: 您的逻辑听起来像ON子句中的OR条件:

SELECT *
FROM table1 t1 INNER JOIN
     table2 t2
     ON t1.ID1 = t2.ID1 OR
        (t1.ID1 IS NULL AND t1.ID2 = t2.ID2)
WHERE t1.ID2 = '234';

You could do a double-left-join to the second table and apply a COALESCE so it something like 您可以对第二个表进行双左联接并应用COALESCE,因此它类似于

SELECT 
      t1.ID1,
      t1.ID2,
      COALESCE( t2a.ID1, t2b.ID1 ) as T2ID1,
      COALESCE( t2a.ID2, t2b.ID2 ) as T2ID2   
   FROM 
      table1 t1
         LEFT JOIN table2 t2a
            ON t1.ID1 = t2a.ID1
           AND NOT t1.ID1 IS NULL
         LEFT JOIN table2 t2b
            ON t1.ID2 = t2b.ID2
           AND t1.ID1 IS NULL
   WHERE 
      t1.ID2 = '234'

So, if the first table T2A has a record, it pulls the values from that row respectively. 因此,如果第一个表T2A有一条记录,它将分别从该行中提取值。 If it is null, then it goes to T2B alias for the fields. 如果为null,则转到该字段的T2B别名。

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

相关问题 如何基于两列联接两个表,以及如何验证两个表中都存在一列 - How to join two tables based on two columns and verifying that one column is existing in both 如何根据条件在JOIN语句中使用CASE联接两个表? - How to use CASE in JOIN statement to join two tables based on condition? 如何构建 mysql 存储过程以根据不同条件加入两个表中的一个 - How do i build a mysql stored procedure to join one out of two tables based on different conditions 根据外键联接两个表的哪个子列? - Join two tables based on a foreign key which a substring of column? 如何联接两个表,然后选择两个不同的列? - How do I join two tables and then select two different columns? MySQL将基于另一列的值在两个列中显示为“ 0”的两个表联接 - MySQL Join two tables displaying '0' in one column based on another column's value 如何根据 2 个 JSON object 列对两个表进行内部连接? - How to Inner Join two Tables based on 2 JSON object columns? 根据具有不同值的顺序连接两列 - Join two columns based on ordering with different values 一列为空时如何联接两个表? - How to join two tables when one column is null? 如何联接两个表并基于不同的列在Mysql中汇总它们的列 - How to Join two tables and sum their columns across in Mysql based on distinct column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM