題目:
620.有趣的電影
表:cinema
±---------------±---------+
| Column Name | Type |
±---------------±---------+
| id | int |
| movie | varchar |
| description | varchar |
| rating | float |
±---------------±---------+
id 是該表的主鍵(具有唯一值的列)。
每行包含有關電影名稱、類型和評級的信息。
評級為 [0,10] 范圍內的小數點后 2 位浮點數。
編寫解決方案,找出所有影片描述為 非 boring (不無聊) 的并且 id 為奇數 的影片。
返回結果按 rating 降序排列。
結果格式如下示例。
示例 1:
輸入:
±--------±----------±-------------±----------+
| 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 |
±--------±----------±-------------±----------+
解釋:
我們有三部電影,它們的 id 是奇數:1、3 和 5。id = 3 的電影是 boring 的,所以我們不把它包括在答案中。
思路:
題目的查詢條件為“所有影片描述為 非 boring (不無聊) 的并且 id 為奇數 的影片”:
a)id 為奇數
b)description不為boring
使用WHERE關鍵詞以上述條件為查詢條件,獲取返回結果的id, movie, description, rating值即可。
代碼:
SELECT id, movie, description, rating
FROM cinema
WHERE description != 'boring' AND id % 2 = 1
ORDER BY rating DESC