0

我有一个 sql 查询,它使用 LEFT JOINS 将几个表连接在一起。从查询中检索到的数据加倍。

询问;

SELECT 

        T.url AS track_url,
        T.name AS track_name,

        S.status,

        E.action,
        E.ID,
        E.timestamp,

        F.ID AS follower_ID,
        F.follows_ID,

        A.name,
        A.DP,
        A.url AS artist_url



      FROM events E

      LEFT JOIN 
      TRACKS T
      ON T.ID = E.ID AND E.action = 'has uploaded a track.'

      LEFT JOIN 
      STATUS S
      ON S.ID = E.ID AND E.action = 'has some news.'

      LEFT JOIN 
      ARTISTS A
      ON A.ID = E.ID


      LEFT JOIN artist_follows F
      ON F.follows_ID = E.ID


      WHERE E.ID != '2'  AND F.ID = '2'

     ORDER BY E.timestamp DESC LIMIT 5

结果(仅显示来自statusAND的数据events);

+--------------------------------------------------------------------+
| status | action        | ID | timestamp | follower_id | follows_id |
+--------------------------------------------------------------------+
| test   | has some news.| 3  | T.1       | 4           | 3          |
| another| has some news.| 3  | T.1       | 4           | 3          |
| test   | has some news.| 3  | T.2       | 4           | 3          |
| another| has some news.| 3  | T.2       | 4           | 3          |
+--------------------------------------------------------------------+

如您所见,来自的数据status翻了一番。T.2属于 status another, T.1 属于 status test。显示的是来自的数据status显示了两次,一次是T.1's来自 的数据集,events一次是来自T.2's的数据集events。这意味着当一个 ID 发布了多个状态时,该状态将与事件表中该 ID 的每一位数据相连接。

4

1 回答 1

0

我不在我的常规计算机上,所以我无法测试它,但你不应该在你的连接上AND替换吗?WHERE

于 2012-09-01T11:17:30.760 回答