2014-10-11 17 views
0

這裏是我的朋友表在SQL enter image description here如何獲得朋友UniqID與SQL查詢

我需要一個查詢給我所有的朋友的用戶的ID,我寫了這個查詢,但它選擇的所有行並非所有的ID

SELECT * from Friend_Table 
where ([email protected] and ReqStatus='True') 
    or 
    [email protected] and ReqStatus='True') 

我的問題是用戶ID可以在發件人ID或reciverID,我只需要在返回表中的一列!

我的輸出表所示:

enter image description here

+0

告訴我們您的預期產品表 – 2014-10-11 10:08:28

+0

@Ganesh_Devlekar我加了! – Domino 2014-10-11 10:11:46

回答

1

您需要選擇SELECT子句中相反的ID。你既可以聯合2個查詢,或使用一個case語句來接:

(注意,未測試的代碼)

select 
    case when [email protected] then ReciverID else SenderID end as OtherPersonID 
from 
    Friend_Table 
where 
    ReqStatus='True' 
    and ([email protected] or [email protected]) 

爲聯合:

select ReciverID as OtherPersonID from Friend_Table where (ReqStatus='True' and [email protected]) 
union 
select SenderID as OtherPersonID from Friend_Table where (ReqStatus='True' and [email protected]) 

而且,正確的拼寫實際上是'接收者'。

+0

謝謝我工作正常 – Domino 2014-10-11 10:23:54