我有一个 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
结果(仅显示来自status
AND的数据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 的每一位数据相连接。