具有主键字段的查询使用了两次 [英] A query with primary key field used twice

查看:123
本文介绍了具有主键字段的查询使用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有两个表:

In my database I have two tables:

关系表: organization_id_first, organization_id_second, relationship_type

组织表: primary key = org_id ; org_id, org_name, ...

我如何能够加入organization表,以便我可以为在关系表中具有条目的两个组织获得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?

谢谢!

推荐答案

这就是我在T-SQL中的处理方式……只需将其加入两次并创建两个不同的对象

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

这篇关于具有主键字段的查询使用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆