2011-09-28 15 views
-1

我有類似這樣SQL - 從沒有多個相同的查找表更新每行的多個字段加入

create table #temp 
(
    log_id int, 
    ...some other fields, 
    one_user_id int, 
    two_user_id int, 
    three_user_id int, 
    four_user_id int, 
    one_username varchar(50), 
    two_username varchar(50), 
    three_username varchar(50), 
    four_username varchar(50) 
) 

我開始了知道所有的用戶ID的臨時表,但後來我需要仰視他們的用戶查找表中的名稱並更新臨時表中的名稱字段。

create table #user_lookup 
(
    user_id int, 
    username varchar(50) 
) 

我知道我可以加入到用戶的查找表一次使用不同的別名,讓他們所有的每一個ID,但我一直在尋找一個靈活的方式來做到這一點只有一次。

任何想法?

編輯:

好吧,更多信息的目的是爲多個用戶每行。 #temp錶行(並非顯示的所有字段)表示一個日誌條目,表示多個用戶可能進行的多個操作的排序規則,但都綁定到該日誌行。

我可能有重複的日誌行,每個用戶扮演一個角色,但更容易在客戶端作爲單行使用。

這就是爲什麼每行有多個用戶。

+4

你需要或者'JOIN'多次或修復您的表設計。 – JNK

+0

我說我知道我可以加入多次,我正在尋找替代品 – Ian

+2

@Ian:JNK的觀點是沒有重新設計你的'#temp'表格結構就沒有別的選擇。 –

回答

1

我認爲這應該工作:

UPDATE temp 
    SET one_username = u1.username 
    , two_username = u2.username 
    , three_username = u3.username 
    , four_username = u4.username 
    FROM #temp as temp 
    join #user_lookup as u1 on u1.user_id = temp.one_user_id 
    join #user_lookup as u2 on u2.user_id = temp.two_user_id 
    join #user_lookup as u3 on u3.user_id = temp.three_user_id 
    join #user_lookup as u4 on u4.user_id = temp.four_user_id 

但我不知道爲什麼你有四個用戶在一個表...;)

+0

這就是我目前正在做的事情。我只是想知道是否有另一種我不熟悉的方式。thx的反饋雖然:) – Ian

+1

對不起,我以爲你正在運行四個更新。因此,如果您進行四次連接,則可以進行四次單獨更新。我認爲四個人更新更好,因爲代碼更具可讀性。 (只有Chuck Norris可以編寫可讀的SQL) – DavidEG

0

唯一的其他真正的替代解決方案是在拉與IN子句相關的記錄並利用CASE語句將用戶名與正確的user_id綁定。然而,這比簡單地使用JOIN聲明更復雜,除了沒有涉及多個JOIN之外,並沒有真正提供任何優勢。下面是一個如何使用這種結構提取數據的完整工作示例:

create table #temp 
(
    one_user_id int, 
    two_user_id int, 
    three_user_id int, 
    four_user_id int, 
    one_username varchar(50), 
    two_username varchar(50), 
    three_username varchar(50), 
    four_username varchar(50) 
) 
insert #temp (one_user_id, two_user_id, three_user_id, four_user_id) values (1, 3, 6, 7) 
insert #temp (one_user_id, two_user_id, three_user_id, four_user_id) values (2, 5, 8, 1) 

;with User_Lookup as (
    select 1 as user_id, 'abc' as username union 
    select 2, 'def' union 
    select 3, 'ghi' union 
    select 4, 'jkl' union 
    select 5, 'mno' union 
    select 6, 'pqr' union 
    select 7, 'stu' union 
    select 8, 'vwx' union 
    select 9, 'jon' union 
    select 10, 'bob' 
), Result as (
    select 
     one_user_id, 
     two_user_id, 
     three_user_id, 
     four_user_id, 
     max(case when U.user_id = one_user_id then U.username end) as one_username, 
     max(case when U.user_id = two_user_id then U.username end) as two_username, 
     max(case when U.user_id = three_user_id then U.username end) as three_username, 
     max(case when U.user_id = four_user_id then U.username end) as four_username 
    from 
     #Temp T, 
     User_Lookup U 
    where 
     U.user_id in (T.one_user_id, T.two_user_id, T.three_user_id, T.four_user_id) 
    group by 
     T.one_user_id, T.two_user_id, T.three_user_id, T.four_user_id 
) 
update 
    #temp 
set 
    one_username = R.one_username, 
    two_username = R.two_username, 
    three_username = R.three_username, 
    four_username = R.four_username 
from 
    Result R 
inner join 
    #temp T on R.one_user_id=T.one_user_id and R.two_user_id=T.two_user_id 
     and R.three_user_id=T.three_user_id and R.four_user_id=T.four_user_id 

select * from #temp 

drop table #temp 

輸出:

one_user_id two_user_id three_user_id four_user_id one_username two_username three_username four_username 
1   3   6    7    abc    ghi    pqr    stu 
2   5   8    1    def    mno    vwx    abc 
相關問題