使用order by和group by的MYSQL子查询
我有两张桌子,联赛和赛季 赛季有使用order by和group by的MYSQL子查询,mysql,sql,Mysql,Sql,我有两张桌子,联赛和赛季 赛季有league\u id,开始日期 我正试图抓住每个联赛的最新赛季。。然后按asc顺序按赛季开始日期排序结果 SELECT * FROM leagues JOIN ( SELECT id as season_id, title as season_title, league_id, start_date FROM seasons ORDER BY start_date DESC) AS seasons ON leagues.id = seasons.
league\u id,开始日期
我正试图抓住每个联赛的最新赛季。。然后按asc顺序按赛季开始日期排序结果
SELECT *
FROM leagues
JOIN (
SELECT id as season_id, title as season_title, league_id, start_date
FROM seasons
ORDER BY start_date DESC) AS seasons
ON leagues.id = seasons.league_id
ORDER BY seasons.start_date ASC;
这件事我已经做了一段时间了。。这种方法很有效,但却能为一个联赛创造多个赛季的记录
我发现了一个使用PHP的解决方案,但我希望只使用MYSQL
非常感谢您的建议。您需要在子查询中使用一个
MAX()
聚合,以获取每个季节的最新开始日期,并将其与季节表连接起来
SELECT
/* Don't SELECT * in production code -
trim this to just the columns you actually need */
leagues.*,
seasons.*
FROM
/* Start with an inner join between seasons and leagues */
leagues
INNER JOIN seasons ON leagues.id = seasons.league_id
INNER JOIN (
/* Subquery gets leage_id and latest start date per league_id group */
SELECT
league_id,
MAX(start_date) AS maxstart
FROM seasons
GROUP BY league_id
/* joining back to seasons on both of those columns to return
the full season column data */
) maxseason
ON seasons.league_id = maxseason.league_id
AND seasons.start_date = maxseason.startdate
ORDER BY seasons.start_date ASC
您需要在子查询中使用MAX()
聚合来获取每个季节的最新start\u日期
,并将其与seasures
表连接起来
SELECT
/* Don't SELECT * in production code -
trim this to just the columns you actually need */
leagues.*,
seasons.*
FROM
/* Start with an inner join between seasons and leagues */
leagues
INNER JOIN seasons ON leagues.id = seasons.league_id
INNER JOIN (
/* Subquery gets leage_id and latest start date per league_id group */
SELECT
league_id,
MAX(start_date) AS maxstart
FROM seasons
GROUP BY league_id
/* joining back to seasons on both of those columns to return
the full season column data */
) maxseason
ON seasons.league_id = maxseason.league_id
AND seasons.start_date = maxseason.startdate
ORDER BY seasons.start_date ASC
您可以非常简单地执行此操作,而无需使用自联接进行子查询:
SELECT s1.*, l.*
FROM seasons s1
LEFT JOIN seasons s2
ON s2.league_id = s1.league_id AND s2.start_date > s1.start_date
JOIN leagues l
ON l.id = s1.league_id
WHERE s2.league_id IS NULL
ORDER BY s1.start_date
此方法的性能通常优于子查询。您可以非常简单地执行此操作,而无需使用自联接进行子查询:
SELECT s1.*, l.*
FROM seasons s1
LEFT JOIN seasons s2
ON s2.league_id = s1.league_id AND s2.start_date > s1.start_date
JOIN leagues l
ON l.id = s1.league_id
WHERE s2.league_id IS NULL
ORDER BY s1.start_date
此方法通常优于子查询。您还可以使用group by:
SELECT *
FROM seasons s1
JOIN
(SELECT l.id as league_id, s.id as season_id, MAX(start_date) as max_start_date
FROM leagues l
JOIN seasons s ON l.id = s.league_id
GROUP BY l.id, s.id) ls ON (ls.season_id = s1.id AND s1.start_date = ls.max_start_date)
ORDER BY s1.start_date ASC
您还可以通过以下方式使用组:
SELECT *
FROM seasons s1
JOIN
(SELECT l.id as league_id, s.id as season_id, MAX(start_date) as max_start_date
FROM leagues l
JOIN seasons s ON l.id = s.league_id
GROUP BY l.id, s.id) ls ON (ls.season_id = s1.id AND s1.start_date = ls.max_start_date)
ORDER BY s1.start_date ASC
我希望我没有弄错你的问题
我希望我没有弄错你的问题。感谢你的快速回复,我去测试了一下,但是我在“on子句”中得到了一个错误未知的列“maxseasons.league\u id”。它只是一个不正确的别名maxseasons
应该是maxseasons
非常感谢您这非常好用。。刚刚还必须将maxseason.startdate更改为maxseason.MaxStart收到快速回复,我去测试了一下,但在“on子句”中得到了一个错误未知的列“maxseasons.league_id”,它只是一个不正确的别名maxseasons
应该是maxseasons
非常感谢您这非常好用。。只需将maxseason.startdate更改为maxseason.maxstart