简体   繁体   English

具有反向联接的MySQL查询

[英]MySql query with reverse joins

I am trying to build a query returning a list of contacts associated with a specific opportunity. 我正在尝试建立一个查询,以返回与特定机会相关联的联系人列表。

I have 3 tables: contacts, opportunities, and relationships (many to many) 我有3个表格:联系方式,机会和关系(很多)

contacts 联络人

id   name
---------
1   Davey Jones
2   Bob Hope
3   Tiger Woods
4   Hillary Clinton

opportunities 机会

id    description
------------------
1     visit the locker
2     singing and dancing
3     playing golf
4     laughing and crying

relationships 关系

id     firstid     firsttype      secondid     secondtype
---------------------------------------------------------
1      1           contact        1           opportunity
2      3           opportunity    3           contact
3      4           contact        4           opportunity
4      4           opportunity    3           contact

Now, if I have the opportunity_id, I want to return all the contacts associated with that opportunity. 现在,如果我有机会,请返回与该机会相关的所有联系人。

So if opportunity_id=4, the result of the successful query should be: 因此,如果机遇_id = 4,则成功查询的结果应为:

Hillary CLinton
Tiger Woods

But this is my query, which only returns 1 record: 但这是我的查询,仅返回1条记录:

SELECT
contacts.name
FROM
contacts
INNER JOIN relationships ON contacts.id = relationships.secondid
INNER JOIN opportunities ON opportunities.id = relationships.firstid
where
opportunities.id=4
and (relationships.firsttype='opportunity' and relationships.secondtype='contact')
or (relationships.firsttype='contact' and relationships.secondtype='opportunity')

I am stuck on how to do flip-flop the joins in this query. 我被困在如何在此查询中翻转联接。

EDIT: I just discovered UNION and then tried this and it seems to work: 编辑:我刚刚发现UNION,然后尝试了此,它似乎工作:

(select contacts.name from contacts where contacts.id =
    (select secondid as id from relationships  where (firstid = 4 and (firsttype='opportunity' and secondTtpe='contact' ) ) ) )
    UNION
 (select contacts.name from contacts where contacts.id =
    (select firstid as id from relationships where (secondid = 4 and (firsttype='contact' and secondtype='opportunity' ) ) ) )

But this seems clunky. 但这似乎很笨拙。 Is this the best way to handle this? 这是处理此问题的最佳方法吗?

Try this: 尝试这个:

SELECT contacts.name FROM contacts
    inner join (
        SELECT * from relationships
        where 
            (firstid = 4 and (firsttype='opportunity' and secondtype='contact' ) )
            or 
            (secondid= 4 and (firsttype='contact' and secondtype='opportunity' ) ) 
    ) rel
    on contacts.id = rel.id;

In a simple way, you need to change the design of your table relationships , I'll suggest: 用一种简单的方法,您需要更改表relationships的设计,我建议:

relationships 关系

id     id_contact     id_opportunity
-------------------------------------
1      1              1
2      3              3
3      4              4
4      3              4

So with the changes on relationships , your query can look like: 因此,随着relationships的变化,您的查询可能类似于:

SELECT contacts.name 
FROM contacts
INNER JOIN relationships ON contacts.id = relationships.id_contact
INNER JOIN opportunities ON opportunities.id = relationships.id_opportunity
WHERE opportunities.id=4;

You must remember to normalize your tables (so the table relationships must have an identifier from the other tables, just one from each table... thats why is an identifier), and be more careful in the many-to-many tables. 你必须记住一定要规范你的表(这样的表relationships必须从其他表的标识,每个表中只是一个 ...这就是为什么是一个标识符),并且是在更加小心many-to-many表。

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

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