好的,所以我正在制作一个家庭影院系统,并显示我一直在选择第一季第一集的电视连续剧列表,并显示该剧集的相关信息,然后你可以向下钻取对其他人。
通过这种方式,我可以将所有电影和所有电视节目保存在一张桌子上,使播放功能更加轻松。
问题是,有些系列我没有数据库中的第一集,我希望它们无论如何都要出现,所以我想选择最小的季节/系列而不是第一季。
我不能将MIN
放在select子句中,因为它只选择一个系列,我不能将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
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分:0)