2012-11-29 74 views
3

我需要連接兩個表才能根據Reason_Id從另一個表中獲取多個字段的Reason_Descriptions。我的問題是,我不知道如何比較字段值。按字段值連接SQL表

Table 1: 
    Reason_Id, 
    Reason_Description 


Table 2: 
    Reason1_Id, 
    Reason2_Id, 
    Reason3_Id 

在表2字段中的值總是匹配來自Reason_Id字段在表1中我只需要顯示說明,而不是ID的值。任何幫助表示讚賞。我知道如何做一個簡單的連接,其中一個表字段匹配另一個,但在這種情況下,每個Reason1,2,3從表2將有不同的原因編號以匹配表1.

+1

您使用的RDBMS是什麼? – Taryn

+0

看看http://sqlfiddle.com/#!3/3b9d1/2 –

回答

2

你必須加入在描述表三次,一次爲每個字段在表2

例如:

SELECT Desc1.Reason_Description AS Reason1_Description, 
     Desc2.Reason_Description AS Reason2_Description, 
     Desc3.Reason_Description AS Reason3_Description 
FROM Table2 
JOIN Table1 Desc1 ON Table2.Reason1_Id = Desc1.Reason_Id 
JOIN Table1 Desc2 ON Table2.Reason2_Id = Desc2.Reason_Id 
JOIN Table1 Desc3 ON Table2.Reason3_Id = Desc3.Reason_Id 
+0

這個工作完美!謝謝。我確信有類似的問題,但我發現很難找到正確的搜索條件來解決這些問題。 – user1864446

0
select * from t1 inner join t2 
on t1.Reason_Id = t2.Reason1_Id 
    or t1.Reason_Id = t2.Reason2_Id 
    or t1.Reason_Id = t2.Reason3_Id 

它看看這裏http://sqlfiddle.com/#!3/3b9d1/2

0
select Reason_Description 
from table1 a 
Inner join table2 b 
    On a.Reason_id = b.Reason1_Id 
Union 
select Reason_Description 
from table1 a 
Inner join table2 b 
    On a.Reason_id = b.Reason2_Id 
Union 
select Reason_Description 
from table1 a 
Inner join table2 b 
    On a.Reason_id = b.Reason3_Id 
0

你基本上可以在UNPIVOT的第二張表上再加上值。如果你沒有一個UNPIVOT功能,那麼你可以使用UNION ALL

select t1.reason_description, t2.col 
from table1 t1 
left join 
(
    select reason1_id value, 't2_reason1_id' col 
    from table2 
    union all 
    select reason2_id value, 't2_reason2_id' col 
    from table2 
    union all 
    select reason3_id value, 't2_reason3_id' col 
    from table2 
) t2 
    on t1.reason_id = t2.value 

如果你有UNPIVOT,那麼你可以使用這樣的事情:

select t1.reason_description 
from table1 t1 
(
    select value, col 
    from table2 
    unpivot 
    (
    value for col in (reason1_id, reason2_id, reason3_id) 
) un 
) t2 
    on t1.reason_id = t2.value 
+0

謝謝。我肯定會使用它! – user1864446

0

我們做了很多這樣的在我的辦公室裏。如果您有基於安全和服務器級別的功能,請使用功能。這可能是在處理器的迭代更昂貴,但如果你沒有巨大的表(我們這樣做是高達數百萬條記錄),內聯標量函數使清潔碼

CREATE FUNCTION dbo.fnc_GetReasonDesc (@ReasonId as int) RETURNS varchar(50) AS 
    BEGIN 
    DECLARE @rDesc as varchar(50) 
    SElECT @rDesc = Reason_Description From Table2 WHERE Reason_id = @ReasonId 
    RETuRN @rDesc 
    END 

Select dbo.fnc_GetReasondesc(Reason1_Id), dbo.fnc_GetReasonDesc(Reason2_Id), ... 

最大的優點是沒有你不管有多少個ID域與或有多少不同的表,他們可能會在工作,你總是可以使用相同的函數從ID轉換遞減。您還可以創建一個枚舉表,包括「描述型」,這樣,如果你有說原因的描述,也問題描述或活動說明等您可以附加字段到你的「描述」表作爲,也就是說,descriptionType添加,然後在你的函數參數中包含該值。現在,同一張表和函數可以處理任何您需要的不同枚舉器。

希望這有助於

0

我相信這個版本應該是大表更有效,因爲只需要一通過表1而不是3:

離開這裏的答案它應該是學術興趣,但它的性能比多個連接選項差,所以請不要使用此:

if OBJECT_ID('Table2') is not null drop table Table2 
if OBJECT_ID('Table1') is not null drop table Table1 


create table Table1 
(
    Reason_Id bigint not null identity(1,1) primary key clustered 
    , Reason_Description nvarchar(256) 
) 
create table Table2 
(
    Id bigint not null identity(1,1) primary key clustered 
    , Reason1_Id bigint foreign key references Table1(Reason_Id) 
    , Reason2_Id bigint foreign key references Table1(Reason_Id) 
    , Reason3_Id bigint foreign key references Table1(Reason_Id) 
) 
insert Table1 select 'Desc 1' 
insert Table1 select 'Desc 2' 
insert Table1 select 'Desc 3' 
insert Table1 select 'Desc 4' 

insert Table2 select 1, 2, 3 
insert Table2 select 4, 4, 4 

select a.id 
, max(case when a.Reason1_Id = b.Reason_Id then b.Reason_Description end) 
, max(case when a.Reason2_Id = b.Reason_Id then b.Reason_Description end) 
, max(case when a.Reason3_Id = b.Reason_Id then b.Reason_Description end) 
from Table2 a 
left outer join Table1 b --could do an inner join but left outer is safer 
on b.Reason_Id in (a.Reason1_Id, a.Reason2_Id, a.Reason3_Id) 
group by a.Id 

這裏有一個SQL小提琴鏈接上面的對比對多表連接選項:http://sqlfiddle.com/#!3/1f5e6/1