通过基于最大值的连接获取整行

Get full row with a join based on max value

在 mysql 我有这两个 tables:

**Seasons**
id
number
current

**Episodes**
id
number
season_id
other_info

我想从 Episodes 中获取整行,从 Seasons 中合并的行全部基于 table.[=19 的最大 number 值=]

例如,如果我有第 1、2、3 季,我想获取最近一季的最新一集。

我从这个查询开始:

SELECT e.id as episode_id, s.id as season_id, MAX(s.number) AS season_number, MAX(e.number) AS episode_number, e.other_info
FROM (episodes AS e)
JOIN seasons AS s ON e.season_id=s.id
WHERE s.current =  1

有点效果,但随后显示错误的 other_info 值。我知道这是因为 MAX() 对行进行了分组,但其他值是随机的,所以我认为它只是从可用的第一行开始显示 other_info

环顾四周,我发现了这个我认为有用的东西,但它没有:

SELECT e.id as episode_id, s.id as season_id, MAX(s.number) AS season_number, MAX(e.number) AS episode_number, e.other_info
FROM (episodes AS e)
JOIN seasons AS s ON e.season_id=s.id
WHERE s.current =  1 AND
e.number = (select max(sube.number) from episodes sube);

在这种情况下,我只得到所有 NULL 值。

我认为这对你有帮助:

SELECT seasons.*, episodes.* 
FROM episodes
INNER JOIN seasons ON episodes.season_id = seasons.id
ORDER BY seasons.number DESC ,episodes.number DESC 
limit 0,1