我有2个表:下面结构中的转移和出口。
转移表
TransID | FromOutlet | ToOutlet
--------| -----------| ----------
T001 | 111 | 222
出口表
OutletCode | OutletDesc
-----------|------------
111 |ABC
222 |XYZ
我想要输出如下。
TransId | FromOutlet | ToOutlet
--------|------------|----------
T001 | ABC | XYZ
我使用下面的查询来检索数据。但是,我没有获得不同的出口,而是为两个列提供了相同的出口名称。
SELECT
ISNULL(Transfers.TransID,'') AS 'TransId',
ISNULL(Transfers.FromOutlet,'') AS 'outletFrom' ,
ISNULL(Outlet.OutletDesc,'') AS 'Outlets',
ISNULL(Transfers.TOOutlet,'') AS 'outletTO',
ISNULL(Outlet.OutletDesc,'') AS 'Outlets'
FROM Transfers INNER JOIN Outlet ON
Transfers.FromOutlet = Outlet.OutletCode
WHERE (Transfers.TransID = 'T001')
答案 0 :(得分:1)
您需要两次加入Outlet表 - 一次用于FromOutleft
,一次用于ToOutlet
:
SELECT TransID,
FromOutlet,
F.OutletDesc AS 'Outlets',
TOOutlet,
T.OutletDesc AS 'Outlets'
FROM Transfers
INNER JOIN Outlet F ON Transfers.FromOutlet = F.OutletCode
INNER JOIN Outlet T ON Transfers.ToOutlet = T.OutletCode
WHERE TransID = 'T001'