您好
我有两张桌子:
情节:
- ID
- 插曲
- 季节
- 系列
- 标题
系列:
- ID
- 名称
我想收到最后添加的剧集(不重复剧集)
例如: 我在“行尸走肉”中添加了两集,向House M.D添加了一集。
所以,结果应该是:
第2集 - 行尸走肉
第1集 - House M.D。
我此时的查询是:
SELECT *, e.id AS episodeId, s.id AS seriesId
FROM episodes AS e
LEFT JOIN series AS s ON e.series = s.id
ORDER BY e.id DESC LIMIT 25
答案 0 :(得分:1)
尝试使用MAX(e.id)
SELECT MAX(e.id) AS episodeId, s.id AS seriesId
FROM episodes AS e
LEFT JOIN series AS s ON e.series = s.id
GROUP BY s.id
如果您需要更多列,请使用连接
SELECT *
FROM episodes AS ep
JOIN
(SELECT MAX(e.id) AS episodeId, s.id AS seriesId
FROM episodes AS e
LEFT JOIN series AS s ON e.series = s.id
GROUP BY s.id) temp
ON temp.episodeId =ep.id