简体   繁体   English

如何使用条件对 mysql 数据库进行不同的连接

[英]How do I do a distinct join on a mysql db using conditions

I have two tables I am trying to match.我有两个我想匹配的表。 I want to have the referredbyname in sourcelist match the name column to targetjoin when the record is in the same groupname当记录在同一个组名中时,我想让 sourcelist 中的referredbyname 匹配 name 列到 targetjoin

I would like to join these two tables with one-one match.我想以一对一的方式加入这两个表。 Contacts in targetjoin have multiple recordtypes with the same name. targetjoin 中的联系人具有多个同名的记录类型。 I want to do some type of distinct join based on a hierarchy set up of record type name我想根据记录类型名称的层次结构设置进行某种类型的不同联接

For example: If a record is matched with the two recordtypes for the same name the RecordType: TypeA will be matched only, and so forth.例如:如果一条记录与同名的两个记录类型匹配,则 RecordType:TypeA 将仅匹配,依此类推。

sourcelist :来源清单:

           ID  GroupName  Name          ReferredbyName        
           1   A          John Smith    Sally Bosh
           2   A          Craig Miller  Sally Smith
           3   A          Fulton Fork   Spoon Knife
           4   B          Joe Sample    George Test

targetjoin :目标连接:

   ID   GroupName     Name         RecordType
   101   A            Sally Bosh   TypeA
   102   A            Sally Bosh   TypeB
   103   A            Sally Smith  TypeC
   104   A            Sally Smith  TypeD
   105   B            George Test  TypeF

My Result:我的结果:

    | id | groupname |         name | referredbyname |  id | groupname |        name | recordtype |
    |----|-----------|--------------|----------------|-----|-----------|-------------|------------|
    |  2 |         A | Craig Miller |    Sally Smith | 103 |         A | Sally Smith |      TypeC |
    |  1 |         A |   John Smith |     Sally Bosh | 102 |         A |  Sally Bosh |      TypeB |
    |  1 |         A |   John Smith |     Sally Bosh | 101 |         A |  Sally Bosh |      TypeA |
    |  2 |         A | Craig Miller |    Sally Smith | 104 |         A | Sally Smith |      TypeD |
    |  4 |         B |   Joe Sample |    George Test | 105 |         B | George Test |      TypeF |

This result gives me all the of possible matches one-many join with duplicate ID这个结果给了我所有可能的匹配一对多连接重复的 ID

I would like to have desired result like this:我想要这样的结果:

    | id | groupname |         name | referredbyname |  id | groupname |        name | recordtype |
    |----|-----------|--------------|----------------|-----|-----------|-------------|------------|
    |  2 |         A | Craig Miller |    Sally Smith | 103 |         A | Sally Smith |      TypeC |
    |  1 |         A |   John Smith |     Sally Bosh | 101 |         A |  Sally Bosh |      TypeA |
    |  4 |         B |   Joe Sample |    George Test | 105 |         B | George Test |      TypeF |

This is what I have gotten so far这是我到目前为止所得到的

select a.*, b.*
from sourcelist a 
join targetjoin b
on a.groupname=b.groupname
and 
case
when b.recordtype in ('TypeA') and a.referredbyname=b.name then 1
when b.recordtype in ('TypeB') and a.referredbyname=b.name then 2
when b.recordtype in ('TypeC') and a.referredbyname=b.name then 3
when b.recordtype in ('TypeD') and a.referredbyname=b.name then 4
when b.recordtype in ('TypeE') and a.referredbyname=b.name then 5
when b.recordtype in ('TypeF') and a.referredbyname=b.name then 6
else 0
end in (1,2,3,4,5,6)
order by a.groupname

Schema: http://sqlfiddle.com/#!9/eb97f架构: http ://sqlfiddle.com/#!9/ eb97f

Thanks for any help!谢谢你的帮助!

SELECT x.* 
  FROM targetjoin x
  JOIN
     ( SELECT name
            , MIN(recordtype) recordtype 
         FROM targetjoin 
        GROUP 
           BY name
     ) y
    ON y.name = x.name
   AND y.recordtype = x.recordtype;

The last part of this problem has been left as an exercise for the reader.这个问题的最后一部分留给读者作为练习。

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

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