2013-03-09 107 views
2

我有2個表,我想從中獲取用戶數據。MySQL選擇查詢不起作用

表1:frei_session

username session_id 

Prince  51 
Dilip  49 
Sumit  50 

表2:用戶之間friendrequest

requestTo requestFrom  status 

49    50    C 
50    51    C 
49    51    P 

friendrequest表顯示關係,其中Ç代表完整P代表待定。意味着如果status = C都是朋友,並且status = P請求正在等待處理。

frei_session表顯示網站的所有在線用戶。

我想限制只與朋友聊天。我曾嘗試下面的查詢,但它顯示了在線

假設用戶的所有用戶在線

SELECT DISTINCT f.username,f.session_id 
    FROM frei_session AS f 
    LEFT JOIN friendrequest AS fr ON fr.requestFrom=f.session_id 
    LEFT JOIN friendrequest AS frnd ON frnd.requestTo=f.session_id 
    WHERE (fr.requestFrom<>49 OR frnd.requestTo<>49) AND (fr.status = 'C' OR frnd.status = 'C') 

這個查詢告訴我輸出這樣

username session_id 

Prince   51 
Sumit   50 

,但我要像輸出這

username session_id 

Sumit  50 

因爲用戶和用戶不是朋友

回答

4
SELECT IF(a.RequestTo = 49, c.userName, b.userName) username, 
     IF(a.RequestTo = 49, c.session_ID, b.session_ID) Session_ID 
FROM friendRequest a 
     INNER JOIN frei_session b 
      ON a.requestTo = b.session_ID 
     INNER JOIN frei_session c 
      ON a.requestFrom = c.session_ID 
WHERE a.status = 'C' AND 
     49 IN (a.requestTo, a.requestFrom) 

結果

╔══════════╦════════════╗ 
║ USERNAME ║ SESSION_ID ║ 
╠══════════╬════════════╣ 
║ Sumit ║   50 ║ 
╚══════════╩════════════╝ 

更新1

DELIMITER $$ 
CREATE PROCEDURE ShowAllUserName(IN _sessionID INT) 
BEGIN 
    SELECT IF(a.RequestTo = _sessionID, c.userName, b.userName) username, 
      IF(a.RequestTo = _sessionID, c.session_ID, b.session_ID) Session_ID 
    FROM friendRequest a 
      INNER JOIN frei_session b 
       ON a.requestTo = b.session_ID 
      INNER JOIN frei_session c 
       ON a.requestFrom = c.session_ID 
    WHERE a.status = 'C' AND 
      _sessionID IN (a.requestTo, a.requestFrom); 
END; 
DELIMITER $$ 
調用過程時

CALL ShowAllUserName(49); 
+0

真棒JW。它的工作就像一個魅力,你很棒。非常感謝你 – 2013-03-09 10:16:54

+1

不用客氣':D'很高興幫助。 – 2013-03-09 10:17:09

+1

你想讓我把它轉換成'存儲過程'嗎? – 2013-03-09 10:18:22

2

爲什麼你加入兩次相同的表?

嘗試

LEFT JOIN friendrequest AS fr ON fr.requestFrom=f.session_id AND fr.requestTo=f.session_id 

比,你可以通過改變你在那裏查詢:

WHERE fr.requestFrom<>49 AND fr.status = 'C' 

我想嘗試GROUP BY fr.status,如果我沒理解你的問題,它可能工作。

2

嘗試使用此查詢:

SELECT DISTINCT f.username,f.session_id 
FROM frei_session AS f 
LEFT JOIN friendrequest AS fr ON f.session_id IN (fr.requestFrom, fr.requestTo) 
WHERE fr.requestFrom = 49 AND fr.status = 'C' 
0

你可以先了解誰收到的請求從迪利普用戶。然後,您將獲得向Dilip發送請求的用戶。之後,你結合這些用戶。 SQL是這樣的:

SELECT fs.username, fs.session_id 
FROM frei_session AS fs, friendrequest AS fr 
WHERE fs.username = "Dilip" 
    AND fs.session_id = fr.requestFrom 
    AND fr.status = "C" 
UNION 
SELECT fs.username, fs.session_id 
FROM frei_session AS fs, friendrequest AS fr 
WHERE fs.username = "Dilip" 
    AND fs.session_id = fr.requestTo 
    AND fr.status = "C" 

我相信這個解決方案更容易理解。

0
SELECT IF(a.RequestTo = 49, c.userName, b.userName) username, 
     IF(a.RequestTo = 49, c.session_ID, b.session_ID) Session_ID 
FROM friendRequest a 
     INNER JOIN frei_session b 
      ON a.requestTo = b.session_ID 
     INNER JOIN frei_session c 

      ON a.requestFrom = c.session_ID 
WHERE a.status = 'C' AND 
     49 IN (a.requestTo, a.requestFrom) 
+0

爲什麼你已經複製了JW的代碼? – 2013-03-29 15:55:59