這是我的查詢加入一個表的多個列到另一個表的單個列單查詢
select u.FirstName as AssignedBy, u1.FirstName as RespondedBy, u2.FirstName as ClosedBy
from Messages M
left join User u on isnull(M.AssignedBy, 0) = u.UserId
left join User u1 on isnull(M.RespondedBy, 0) = u1.UserId
left join User u2 on isnull(M.closedby, 0) = u2.UserId
是否有可能寫在一個查詢中加入?我想提高查詢的性能,所以我不想讓3個連接在單個連接中嘗試,因爲我的用戶表有很多記錄。我有兩個表上的所有索引。我試圖
select u.FirstName as AssignedBy, u.FirstName as RespondedBy, u.FirstName as ClosedBy
from Messages M
left join User u on
(isnull(M.AssignedBy, 0) = u.UserId or isnull(M.RespondedBy, 0) = u.UserId or isnull(M.closedby, 0) = u.UserId)
但此查詢給出了,因爲我有這在所有三個連接列記錄多行不正確的輸出。有沒有其他辦法可以達到同樣的效果?
你的表現有多糟糕?你可以用查詢的執行計劃編輯你的問題嗎? – 2014-10-30 11:20:29