将同一个表中的两个字段连接到另一个表中的一个字段

时间:2022-07-21 01:58:53

I'm working on a way to lookup the description for a code that appears in two fields of the same table.

我正在研究一种方法来查找在同一表的两个字段中出现的代码的描述。

The table/field names are :

表/字段名为:

Contacts 
Name, Group_1 and Group_4

Lookup
Lookup_Id, Lookup_Name

Contact.Group_1 and Contact.Group_4 both refer to values in Lookup.Lookup_Id and need to be resolved to their corresponding name values in Lookup.Lookup_Name.

接触。Group_1和联系。Group_4都引用查找中的值。Lookup_Id,需要在Lookup.Lookup_Name中解析为它们对应的名称值。

How can I connect both fields to the Lookup table and have them bring back their respective Lookup_name values ?

如何将两个字段连接到查找表,并让它们返回各自的Lookup_name值?

2 个解决方案

#1


4  

Left Join Contacts with Lookup twice. Once with Group_1 and once with Group_2. Left Join instead of just Inner Join, as you may have a contact without two groups.

左连接联系人查找两次。一次是Group_1,一次是Group_2。左连接而不是内连接,因为您可能有一个没有两个组的联系人。

SELECT C.Name,
       G1.Lookup_Name,
       G2.Lookup_Name
FROM   Contacts C
       LEFT JOIN Lookup G1 ON G1.Lookup_Id = C.Group_1
       LEFT JOIN Lookup G2 ON G2.Lookup_Id = C.Group_4

#2


1  

Like this:

是这样的:

select *
from Contacts c
left join Lookup l1 on l1.Lookup_Id = c.Group_1
left join Lookup l2 on l2.Lookup_Id = c.Group_4

#1


4  

Left Join Contacts with Lookup twice. Once with Group_1 and once with Group_2. Left Join instead of just Inner Join, as you may have a contact without two groups.

左连接联系人查找两次。一次是Group_1,一次是Group_2。左连接而不是内连接,因为您可能有一个没有两个组的联系人。

SELECT C.Name,
       G1.Lookup_Name,
       G2.Lookup_Name
FROM   Contacts C
       LEFT JOIN Lookup G1 ON G1.Lookup_Id = C.Group_1
       LEFT JOIN Lookup G2 ON G2.Lookup_Id = C.Group_4

#2


1  

Like this:

是这样的:

select *
from Contacts c
left join Lookup l1 on l1.Lookup_Id = c.Group_1
left join Lookup l2 on l2.Lookup_Id = c.Group_4