MySQL选择查询不起作用

时间:2013-03-09 09:57:54

标签: mysql sql select join

我有2个表,我想从中获取用户数据。

表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 表显示 C 代表完成 P 代表待定的用户之间的关系即可。表示status = C两者都是朋友,status = P请求是否待处理。

frei_session 表格显示网站的所有在线用户。

我想限制与朋友聊天。我尝试了以下查询,但它显示了所有在线用户

假设用户 49 在线

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

因为用户 51 且用户 49 不是朋友

5 个答案:

答案 0 :(得分: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)

RESULT

╔══════════╦════════════╗
║ 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);

答案 1 :(得分: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 :(得分: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'

答案 3 :(得分:0)

您可以先收到Dilip收到请求的用户。然后,您将获得向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"

我相信这个解决方案更容易理解。

答案 4 :(得分: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)