1

我试图用这个声明做的是显示所有在 1999 年、2000 年和 2001 年上映的超过三个小时的电影(实际的列以秒为单位)。

输出表显示了正确的年份,但它显示了 title_type 的其他内容而不是电影,并且它在 title_runtime_hrs 中显示的时间少于三(以及多于三)。

这是我的代码:

select title_name, title_type, title_release_year, 
    (title_runtime / 3600.0) as title_runtime_hrs
from nf_titles
where title_type = 'Movie' and
    title_runtime > 3 and
    title_release_year = '1999' or
    title_release_year = '2000' or
    title_release_year = '2001' 
order by title_release_year, title_runtime_hrs desc
4

3 回答 3

5

或者这样做

select title_name, title_type, title_release_year, (title_runtime / 3600.0) as title_runtime_hrs
from nf_titles
where title_type = 'Movie' and
    title_runtime > 3 and
    title_release_year in ('1999','2000','2001')
order by title_release_year, title_runtime_hrs desc
于 2013-10-11T14:11:35.890 回答
3

您需要将您的OR陈述用括号括起来。否则 SQL Server 会说“好的,这是 2000 年,我们已经满足条件。

select title_name, title_type, title_release_year, (title_runtime / 3600.0) as title_runtime_hrs
from nf_titles
where title_type = 'Movie' and
    title_runtime > 3 and
    (title_release_year = '1999' or
    title_release_year = '2000' or
    title_release_year = '2001' )
order by title_release_year, title_runtime_hrs desc
于 2013-10-11T14:07:52.357 回答
2

您的OR语句可能需要括号(这是因为AND绑定强于OR):

select title_name, title_type, title_release_year, (title_runtime / 3600.0) as title_runtime_hrs
from nf_titles
where title_type = 'Movie' and
    title_runtime > 3 and
    (
        title_release_year = '1999' or
        title_release_year = '2000' or
        title_release_year = '2001' 
    )
order by title_release_year, title_runtime_hrs desc
于 2013-10-11T14:08:02.850 回答