A query with primary key field used twice

非 Y 不嫁゛ 提交于 2019-12-11 12:23:34

问题


In my database I have two tables:

relationship table: organization_id_first, organization_id_second, relationship_type

organization table: primary key = org_id ; org_id, org_name, ...

How would I be able to join the organization table so that I could get the org_name for both organizations that have an entry in the relationship table? I don't think I can join on the same primary key. Would I have to do a subquery of some sort?

Thanks!


回答1:


This is how i would do it in T-SQL ... just join it twice and make two different object

select or1.org_name, or2.org_name, rel.relationship_type from relationship  rel
   join organization  or1 on rel.organization_id_first = or1.org_id
   join organization  or2 on rel.organization_id_second = or2.org_id


来源:https://stackoverflow.com/questions/35557789/a-query-with-primary-key-field-used-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!