LeetCode:620.有趣的电影

题目链接:https://leetcode-cn.com/problems/not-boring-movies/

题目

某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 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 |
+---------+-----------+--------------+-----------+
对于上面的例子,则正确的输出是为:

+---------+-----------+--------------+-----------+
| id | movie | description | rating |
+---------+-----------+--------------+-----------+
| 5 | House card| Interesting| 9.1 |
| 1 | War | great 3D | 8.9 |
+---------+-----------+--------------+-----------+

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

解答

第一次尝试

---- oracle ----
/* Write your PL/SQL query statement below */
select id,
       movie,
       description,
       rating
from cinema
where description <> 'boring'
and mod(id, 2) <> 0
order by rating desc ---- 796ms

使用mod()函数,通过mod(id, 2) = 1来确定奇数。

---- MySQL ----
select *
from cinema
where mod(id, 2) = 1
and description != 'boring'
order by rating desc;  ---- 99ms

也可以通过位判断的方法

---- MySQL ----
select *
from cinema
where id & 1
and description <> 'boring'
order by rating desc;  ---- 102ms

思考

oracle中用mod(a, b)函数进行模运算。

MySQL中可以使用 id % 2 = 1的普通操作,还有按位与 id & 1这种神操作。

猜你喜欢

转载自www.cnblogs.com/hider/p/11723667.html
今日推荐