【Leetcode_MySQL】620. Not Boring Movies

題目提供了一個列表,我們需要從奇數的電影中排除不有趣的電影,然後把有趣的電影以表的形式輸出。

leetcode 題目在這:https://leetcode.com/problems/not-boring-movies/

Table: Cinema

+----------------+----------+
| Column Name    | Type     |
+----------------+----------+
| id                      | int          |
| movie               | varchar   |
| description       | varchar   |
| rating               | float        |
+----------------+----------+
id is the primary key for this table.
Each row contains information about the name of a movie, its genre, and its rating.
rating is a 2 decimal places float in the range [0, 10]

Write an SQL query to report the movies with an odd-numbered ID and a description that is not "boring".

Return the result table in descending order by rating.

The query result format is in the following example:

解答1:

Leetcode 的官方答案,執行時間:427 ms。

select *
from cinema
where mod(id, 2) = 1 and description != 'boring'
order by rating DESC
;

我把關鍵字都改成了大寫,執行時間: 167 ms。

把關鍵字都改成了大寫後執行效率大幅提升,也不知道是甚麼原因,如果你知道的話可以在底下留言。

SELECT *
FROM cinema
WHERE mod(id, 2) = 1 AND description != 'boring'
ORDER BY rating DESC
;

解答2:

SELECT * 
FROM cinema 
WHERE (id % 2 = 1) AND (description <> 'boring') 
ORDER BY rating DESC

其中 <> 的意思代表著不等於,也就是 “!=”,description <> ‘boring’ 和 description != ‘boring’ 是完全一樣的意思。

Leave a Comment

Your email address will not be published. Required fields are marked *