简体   繁体   English

SQL在两个不同的列上进行联接

[英]SQL to JOIN on two different columns

Our CRM software has a relationship function which allows you to define a relationship between two contacts. 我们的CRM软件具有关联功能,可让您定义两个联系人之间的关联。 The contacts are stored in the CONTACT table and the relationship is stored in CONTACT_CONTACT . 联系人存储在CONTACT表中,关系存储在CONTACT_CONTACT

We relate a doctor contact to a patient contact. 我们将医生联系方式与患者联系方式联系起来。 In the CONTACT_CONTACT table there is CONTACTID1 , CONTACTID2 , CONTACT1_ROLE , and CONTACT2_ROLE . CONTACT_CONTACT桌子上有CONTACTID1CONTACTID2CONTACT1_ROLECONTACT2_ROLE

We query the CONTACT table for the initial CONTACT by using a ClientID. 我们使用ClientID在CONTACT表中查询初始CONTACT Then we use the CONTACTID to do aa table join. 然后,我们使用CONTACTID进行表CONTACTID The problem here is that the CONTACTID we have could be in CONTACT_CONTACT.CONTACTID1 or CONTACT_CONTACT.CONTACTID2 columns. 这里的问题是我们拥有的CONTACTID可能位于CONTACT_CONTACT.CONTACTID1CONTACT_CONTACT.CONTACTID2列中。

SELECT c2.CONTACT,
   c2.ClientID,
   c2.CONTACTID
FROM CONTACT as c
INNER JOIN CONTACT_CONTACT as cc on c.CONTACTID = cc.CONTACTID2
INNER JOIN CONTACT as c2 on cc.CONTACTID1 = c2.CONTACTID
WHERE c.ClientID = 121695 AND (cc.CONTACT1_ROLE = 'Doctor' OR cc.CONTACT2_ROLE = 'Doctor')

Same issue for joining back to the CONTACT table for the contact with the Doctor role listed in CONTACT_CONTACT . 返回到CONTACT表以与CONTACT_CONTACT中列出的Doctor角色进行联系的同一个问题。

If the contact or doctor can be in either CONTACT_CONTACT.CONTACTID1 or CONTACTID2 , what would be the best way to go about this without the possibility of changing the database tables? 如果联系人或医生可以位于CONTACT_CONTACT.CONTACTID1CONTACTID2 ,那么在没有可能更改数据库表的情况下,最好的方法是什么?

COALESCE should work. COALESCE应该起作用。 However, if you have values in both fields, the join will execute on the field you list first inside the parentheses. 但是,如果两个字段中都有值,则连接将在括号内首先列出的字段上执行。

SELECT c.CONTACT,
   c.ClientID,
   c.CONTACTID
FROM CONTACT as c
INNER JOIN CONTACT_CONTACT as cc on c.CONTACTID = COALESCE(cc.CONTACTID1,cc.CONTACTID2)
WHERE c.ClientID = 121695 AND (cc.CONTACT1_ROLE = 'Doctor' OR cc.CONTACT2_ROLE = 'Doctor')

Couldn't you just do the query twice? 您不能只两次查询吗? There would be other ways to be sure, but this should get the information I think: 还有其他方法可以确定,但这应该获得我认为的信息:

SELECT c.CONTACT,
   c.ClientID,
   c.CONTACTID
FROM CONTACT as c
INNER JOIN CONTACT_CONTACT as cc on c.CONTACTID = cc.CONTACTID1
INNER JOIN CONTACT as c2 on cc.CONTACTID2 = c2.CONTACTID
WHERE c.ClientID = 121695 
    AND cc.CONTACT1_ROLE = 'Doctor'
union
SELECT c2.CONTACT,
   c2.ClientID,
   c2.CONTACTID
FROM CONTACT as c
INNER JOIN CONTACT_CONTACT as cc on c.CONTACTID = cc.CONTACTID2
INNER JOIN CONTACT as c2 on cc.CONTACTID1 = c2.CONTACTID
WHERE c.ClientID = 121695 
    AND cc.CONTACT2_ROLE = 'Doctor'

I'm not really clear on what information you're hoping/attempting to get from your query, as you're only selecting from a single table's data. 我不清楚您希望/尝试从查询中获取什么信息,因为您仅从单个表的数据中进行选择。

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

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