我有一个使用 LEFT JOINS 从多个表中选择数据的查询。问题是数据被复制。
这是查询
SELECT
A.ID,
T.T_ID,
T.name,
T.pic,
T.timestamp AS T_ts,
(SELECT COUNT(*) FROM track_plays WHERE T_ID = T.T_ID) AS plays,
(SELECT COUNT(*) FROM track_downloads WHERE T.T_ID) AS downloads,
S.S_ID,
S.status,
S.timestamp AS S_ts,
G.G_ID,
G.gig_name,
G.date_time,
G.lineup,
G.price,
G.currency,
G.pic AS G_pic,
G.ticket,
G.venue,
G.timestamp AS G_ts
FROM artists A
LEFT JOIN TRACKS T
ON T.ID = A.ID
LEFT JOIN STATUS S
ON S.ID = A.ID
LEFT JOIN GIGS G
ON G.ID = A.ID
WHERE A.ID = '$ID'
ORDER BY S_ts, G_ts AND T_ts DESC LIMIT 20
问题是如果连接中的一个表的数据多于另一个,则数据会重复。因此,如果轨道有 1 行,状态有 2 并且 gigs 没有行,您将从轨道中获得的数据加倍。
我尝试过使用GROUP BY A.ID
,但这会消除数据。因此,在前面给出的示例中,只有一行status
显示。
我也尝试过GROUP_CONCAT
,但不确定该功能,所以不能告诉你太多。
USINGSELECT DISTINCT
与GROUP BY A.ID
.