力扣620(MySQL)-有趣的电影(简单)

题目:

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

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

例如,下表cinema:

 对于上面的例子,则正确的输出是为:

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

解题思路:

建表语句:

1 create table if not exists cinema_620(id int(3),movie varchar(15),description varchar(15),rating float(2,1));
2 truncate table cinema_620;
3 insert into cinema_620 values(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);

重点在于:影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片==>description <> 'boring' and id & 2 = 1,结果请按等级 rating 排列==> orser by rating desc。

奇数的判断:id & 2 = 1 偶数: id & 2 = 0

1 select *
2 from cinema_620
3 where description <> 'boring' and id & 1 = 1
4 order by rating desc;

或者:

奇数的判断:id % 2 = 1 偶数: id % 2 = 0

1 select *
2 from cinema_620
3 where description <> 'boring' and id % 2 = 1
4 order by rating desc;

posted on 2023-04-06 09:18  我不想一直当菜鸟  阅读(25)  评论(0编辑  收藏  举报