寻找普通孩子或父母的所有相关记录

时间:2017-03-15 06:09:20

标签: sql postgresql

我有一张表Identity(id, super_identity_id)和一张表Conversation(id, identity_id)

多个身份可以引用相同的super_identity

说我有identity A,我想得到所有conversations

  1. 谁属于A
  2. 和谁属于super_identity
  3. A
  4. 和谁属于identitysuper_identity
  5. 相同的A

    我能做的最好的事情是

    select 
        i.id,
        i.super_identity_id
    from identities i 
    left join identities ii on ii.super_identity_id = i.super_identity_id
    where ii.id = 133
    

    但上述内容并不尊重(2)

    有什么想法吗?

1 个答案:

答案 0 :(得分:0)

select 
i.id,
i.super_identity_id
from identities i 
inner join identities ii on ii.super_identity_id = i.super_identity_id and ii.id = 133