最有趣的电影(SQL 中 order by 的使用)

题目: 某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述。

作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片,结果请按等级 rating 排列。

例如,下表 cinema:

±--------±----------±-------------±----------+
| id | movie | description | rating |
±--------±----------±-------------±----------+
| 1 | War | great 3D | 8.9 |
| 2 | Science | fiction | 8.5 |
| 3 | irish | boring | 6.2 |
| 4 | Ice song | Fantacy | 8.6 |
| 5 | House card| Interesting| 9.1 |
±--------±----------±-------------±----------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/not-boring-movies
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题方法
先给出代码吧:

select id ,movie,description,rating
 from cinema
 where description != 'boring' and id % 2 != 0
 order by rating DESC

主要是要记住 order by 的使用
这个的最开始学习是在group by 的分组结果中进行排序 ,
默认为升序
降序为:DESC

第二种方法:


select id ,movie,description,rating
from (
    select id ,movie,description,rating
    from cinema
    where description !='boring'
)as temp
where id % 2 !=0
order by rating DESC

猜你喜欢

转载自blog.csdn.net/qq_43964318/article/details/109594232