MYSQL 对 DISTINCT 行的 COUNT 个限制

MYSQL LIMIT on COUNT of DISTINCT row

有没有办法将 LIMIT 设置为等于连续 DISTINCT 个条目中的 COUNT?我有以下查询,除了 LIMIT 外,它都有效。我的 table 将有不同数量的 DISTINCT 系列 ID,我想要每个不同系列 ID 中最后一季的最后一集。

SELECT t1.* FROM myTable.SeriesEpisodes t1 JOIN (SELECT seriesID, MAX(Season) Season FROM myTable.SeriesEpisodes GROUP BY seriesID) t2 ON t1.Season = t2.Season AND t1.seriesID = t2.seriesID ORDER BY Episode DESC LIMIT ???

我尝试执行如下子查询,但出现错误。

LIMIT (SELECT COUNT(DISTINCT seriesID) FROM myTable.SeriesEpisodes)

此查询可能return您指定的结果。内联视图 t2 return 是每个系列的最大赛季。内联视图 t3 return 是每个系列每个季节的最大剧集。内联视图(派生的 tables)的结果可以与原始 table 相结合,以检索与那些最大值关联的详细信息行。 (这假定 (Series,Season,Episode) 元组是唯一的。)

SELECT t1.*
  FROM myTable.SeriesEpisodes t1
  JOIN ( SELECT s2.Series
              , MAX(s2.Season) AS Season
           FROM myTable.SeriesEpisodes s2
          GROUP
             BY s2.Series
       ) t2
    ON t2.Series  = t1.Series
   AND t2.Season  = t1.Season
  JOIN ( SELECT s3.Series
              , s3.Season
              , MAX(s3.Episode) AS Episode
           FROM myTable.SeriesEpisodes s3
          GROUP
             BY s3.Series
              , s3.Season
       ) t3
    ON t3.Series  = t1.Series
   AND t3.Season  = t1.Season
   AND t3.Episode = t1.Episode
 ORDER 
    BY t1.Series
     , t1.Season

编辑

--或--

可以使用这样的查询 return 得到等效的结果。内嵌视图 t2 returns 每个系列最新一季的最新一集。

SELECT t1.*
  FROM myTable.SeriesEpisodes t1
  JOIN ( SELECT s2.Series
              , s2.Season
              , MAX(s2.Episode) AS Episode
           FROM myTable.SeriesEpisodes s2
           JOIN ( SELECT s3.Series
                       , MAX(s3.Season) AS Season
                    FROM myTable.SeriesEpisodes s3
                   GROUP
                      BY s3.Series
                ) t3
             ON t3.Series  = s2.Series
            AND t3.Season  = s2.Season
          GROUP
             BY s2.Series
              , s2.Season
       ) t2
    ON t2.Series  = t1.Series
   AND t2.Season  = t1.Season
   AND t2.Episode = t1.Episode
 ORDER 
    BY t1.Series
     , t1.Season