MySQL删除子查询中的重复子句

时间:2014-05-20 19:58:22

标签: mysql sql join distinct union

我正在尝试简化我的查询,以便它只包含一次会话ID(SID)。

Users表的抽象结构是:

+----+------+----------+
| ID | Name | Username |
+----+------+----------+

Friends表有一个抽象结构,如:

+----+-----------------+----------+--------+---------+
| ID |     UserID      | FriendID | Hidden | Deleted |
|    | (Foreign key    |          |        |         |
|    | of ID in Users) |          |        |         |
+----+-----------------+----------+--------+---------+

Sessions表的抽象结构:

+----+-----------------+-----+
| ID |     UserID      | SID |
|    | (Foreign key    |     |
|    | of ID in Users) |     |
+----+-----------------+-----+

我有以下查询,它是根据我之前的一个问题的答案改编的。如您所见,会话ID(SID)重复4次,是否可以将查询压缩为一个整体,以便只需要一次SID?

SELECT *
    ,CASE 
        WHEN D.ID IS NULL
            THEN "Wants to be your friend"
        ELSE "Friends"
        END AS STATUS
FROM (
    SELECT DISTINCT A.ID
        ,A.NAME
        ,E.Hidden
    FROM Users A
    INNER JOIN Friends E ON A.ID = E.UserID
    WHERE A.ID IN (
            SELECT A.UserID
            FROM Friends A
            INNER JOIN Sessions S ON A.FriendID = S.UserID
            WHERE S.SID = "1234"
                AND Deleted = 'No'
            )
    ) C
LEFT JOIN (
    SELECT DISTINCT B.ID
        ,B.NAME
        ,F.Hidden
    FROM Users B
    INNER JOIN Friends F ON B.ID = F.FriendID
    WHERE B.ID IN (
            SELECT A.FriendID
            FROM Friends A
            INNER JOIN Sessions S ON A.UserID = S.UserID
            WHERE S.SID = "1234"
                AND Deleted = 'No'
            )
    ) D ON C.ID = D.ID

UNION

    DISTINCT
SELECT *
    ,CASE 
        WHEN C.ID IS NULL
            THEN "Request Sent"
        ELSE "Friends"
        END AS STATUS
FROM (
    SELECT DISTINCT A.ID
        ,A.NAME
        ,E.Hidden
    FROM Users A
    INNER JOIN Friends E ON A.ID = E.UserID
    WHERE A.ID IN (
            SELECT A.UserID
            FROM Friends A
            INNER JOIN Sessions S ON A.FriendID = S.UserID
            WHERE S.SID = "1234"
                AND Deleted = 'No'
            )
    ) C
RIGHT JOIN (
    SELECT DISTINCT B.ID
        ,B.NAME
        ,F.Hidden
    FROM Users B
    INNER JOIN Friends F ON B.ID = F.FriendID
    WHERE B.ID IN (
            SELECT A.FriendID
            FROM Friends A
            INNER JOIN Sessions S ON A.UserID = S.UserID
            WHERE S.SID = "1234"
                AND Deleted = 'No'
            )
    ) D ON C.ID = D.ID

解释系统的基本方法是,如果两个用户是朋友,那么数据库中有两条记录。一个从第一个用户到另一个用户,另一个记录从第二个用户到第一个用户。

如果有当前用户到另一个用户的记录,则已发送好友请求,如果有一个用户到当前用户的记录,则已收到好友请求。

这是一个如何运作的范围图:

Venn Diagram

SQL小提琴 - http://sqlfiddle.com/#!2/c5587/1

1 个答案:

答案 0 :(得分:0)

Sql fiddle:http://sqlfiddle.com/#!2/06e08/68/0

此返回Friends and Request Sent

SELECT 
  f.FriendID,
  u.Name,
  f.Hidden,
  CASE
    WHEN reqs.FriendID IS NULL
      THEN "Request Sent"
    WHEN reqs.FriendID = f.UserID
      THEN "Friends"
  END AS Status
FROM
  Friends AS f
  INNER JOIN
    Sessions AS s
    ON f.UserId = s.UserID
  INNER JOIN
    Users AS u
    ON u.ID = f.FriendID
  LEFT JOIN
    Friends AS reqs
    ON reqs.FriendID = f.UserID 
      AND reqs.UserID = f.FriendID
WHERE
  s.SID = "sid1"

如果您还需要Request Received,请附加:

UNION

SELECT
  f.UserID,
  u.Name,
  f.Hidden,
  "Request Received" AS Status
FROM
  Friends AS f
  INNER JOIN
    Sessions AS s
    ON f.FriendID = s.UserID
  INNER JOIN
    Users AS u
    ON u.ID = f.UserID
WHERE 
  f.UserID NOT IN 
    (
      SELECT 
        ff.FriendID 
      FROM 
        Friends AS ff 
        INNER JOIN
          Sessions AS ss
          ON ff.UserID = ss.UserID
      WHERE ss.SID = "sid1"
    )
  AND s.SID = "sid1"

无法弄清楚如何优化最后一部分。因为它是一个自我加入,它是一个该死的心灵缠绕。

我知道这不是您的预期,但我无法驾驭所有SID,但此请求应该比您目前使用的请求更快