2

好的,所以我正在开发家庭影院系统,为了显示电视剧列表,我一直在选择第一季的第一集并显示该剧集的相关信息,然后您可以深入了解其他内容。

这样我就可以将所有电影和所有电视节目保存在一个表格中,这使得播放功能更加容易。

问题是,有些系列我在数据库中没有第一集,我希望它们无论如何都会出现,所以我想选择最小的季节/系列而不是第一集。

我不能放入MINselect 子句,因为它只选择一个系列,我不能放入LEAST子句WHERE,因为它说它只是一个元素,因为它们的分组方式,我不能再使用LIMIT因为那么它只会选择一个系列。

有没有办法在没有多个复杂子查询的情况下做到这一点?

这是我目前正在使用的查询,该CASE子句是从系列标题中删除 A/An/The 以获得正确的字母顺序:

SELECT *, CASE when substring(series,1,4) = 'The ' then substring(series, 5)
               when substring(series,1,3) = 'An ' then substring(series, 4)
               when substring(series,1,2) = 'A ' then substring(series, 3)
               else series END AS sorttitle 
FROM Theater.Videos WHERE type='TV' and season=1 and episode=1 ORDER BY sorttitle ASC

这基本上就是我想要的:

SELECT *, CASE when substring(series,1,4) = 'The ' then substring(series, 5)
               when substring(series,1,3) = 'An ' then substring(series, 4)
               when substring(series,1,2) = 'A ' then substring(series, 3)
               else series END AS sorttitle 
FROM Theater.Videos WHERE type='TV' and season=MIN(season) and episode=MIN(episode) ORDER BY sorttitle ASC
4

3 回答 3

1

您可以JOIN使用子查询将表添加到自身MIN()- 这应该是关闭的:

SELECT V.*, CASE when substring(V.series,1,4) = 'The ' then substring(V.series, 5)
               when substring(V.series,1,3) = 'An ' then substring(V.series, 4)
               when substring(V.series,1,2) = 'A ' then substring(V.series, 3)
               else V.series END AS sorttitle 
FROM Theater.Videos V
    JOIN (
        SELECT series, Min(season) MinSeason, MIN(Episode) MinEpisode
        FROM Theater.Videos
        GROUP BY series
    ) t ON t.series = V.series 
        AND t.season = V.MinSeason 
        AND t.episode = V.MinEpisode
WHERE V.type='TV' 
ORDER BY V.sorttitle ASC
于 2013-07-30T04:40:38.420 回答
1

尝试这些查询(经过适当的修改)...

要获取所有系列的列表以及第一(分钟)季的第一集,请使用此...

select distinct v1.*
from videos v1,
    (select series, min(season) as min_season from videos group by series) as v2,
    (select series, season, min(episode) as min_episode from videos group by series, season) as v3
where v1.series = v2.series
and v1.season = v2.min_season
and v2.series = v3.series
and v2.min_season = v3.season
and v1.episode = v3.min_episode

要获取所有系列的列表以及每个季节的第一集,请使用此...

select distinct v1.*
from videos v1,
    (select series, season, min(episode) as min_episode from videos group by series, season) as v3
where v1.series = v3.series
and v1.season = v3.season
and v1.episode = v3.min_episode
于 2013-07-30T07:36:16.573 回答
0

如果没有看到数据库结构,很难说些什么。如果可以,请将您的数据库表的示例概述。
或将 where 条件部分分配给变量并首先检查没有第一集的系列并使用 while 循环获取结果。在 while 结束后,根据您的逻辑执行下一个 where 条件。

于 2013-07-30T05:40:17.027 回答