mySQL - 在连接中加倍数据

时间:2012-09-01 11:14:27

标签: mysql

我有一个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

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

+--------------------------------------------------------------------+
| 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属于状态another,T.1属于状态test。显示的是status的数据显示两次,一次是来自T.1's的{​​{1}}数据集,另一次是来自events的{​​{1}}数据集。这意味着当ID发布了多个状态时,状态将与事件表中该ID的每一位数据相结合。

1 个答案:

答案 0 :(得分:0)

我不在常规计算机上,所以我无法对其进行测试,但是您不应该在联接中用AND替换WHERE吗?