2

我有一个名为 INVOICE 的表,来自我获得 ADDRESSID 的位置

我有另一个名为 Address 的表

ADDRESSID         ADDRESS
2                 BB
2                 BC

并且 CC 和 BC 的值在另一个名为 ADDRESSVALUE 的表中,基于 ADDRESSID

ID      CODE    VALUE
1        BB      BlindFold
2        BC      Bucharest

现在我使用内部连接查询表

SELECT AC.VALUE AS ADDRESS 
FROM ADDRESS AS OT
INNER JOIN INVOICE AS OM ON  OM.ADDRESSID = OT.ADDRESSID
INNER JOIN ADDRESSVALUE AS AC ON OT.ADDRESS = AC.CODE
WHERE OM.ADDRESSID = 2

我得到的结果是

Blindfold
Blindfold

但应该是

BlindFold
Bucharest
4

4 回答 4

1

(更新)获得您正在寻找的结果的一种方法:

SELECT AC.VALUE AS ADDRESS 
FROM ADDRESS AS OT
INNER JOIN INVOICE AS OM ON  OM.ADDRESSID = OT.ADDRESSID
INNER JOIN ADDRESSVALUE AS AC ON OT.ADDRESS = AC.CODE
WHERE OM.ADDRESSID = @AddressID

SQLFiddle在这里

于 2013-07-30T07:46:36.707 回答
0

无需加入即可直接使用!

SELECT OM.VALUE AS ADDRESS 
FROM INVOICE AS OM WHERE OM.ADDRESSID = @AddressID

--

SELECT value FROM second_table WHERE code IN
(SELECT address FROM first_table WHERE addressid = @AddressID);
于 2013-07-30T07:37:20.260 回答
0

尝试这个

SELECT C.VALUE AS ADDRESS
FROM ADDRESS A
INNER JOIN INVOICE B ON A.ADDRESSID=B.ADDRESSID
INNER JOIN ADDRESSVALUE C ON A.ADDRESSID=C.ADDRESSID 
    AND A.ADDRESS=C.CODE
WHERE B.ADDRESSID=2

例如,您可以看到 SQLFiddle

于 2013-07-30T11:40:43.187 回答
0

如果 Address 和 Addressvalue 表应该通过两个表中存在的 ADDRESSID 列联合起来,那么您的 select 语句应该包括类似

ADDRESS.ADDRESSID = ADDRESSVALUE.ADDRESSID
于 2013-07-30T07:47:16.077 回答