leetcode刷題(2021/1/20)
阿新 • • 發佈:2021-01-20
一道簡單的mysql題,因為自己之前sql只會寫最簡單最簡單的增刪改查,通過此題,我學到了一些sql中最常用的萬用字元等等。
題目描述
某城市開了一家新的電影院,吸引了很多人過來看電影。該電影院特別注意使用者體驗,專門有個 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 | +---------+-----------+--------------+-----------+
解答1:
SELECT id,movie,description,rating from cinema where id & 1 and description <> 'boring' order by rating DESC;
解答2:
SELECT id,movie,description,rating from cinema where description != 'boring' and id % 2 = 1 order by rating DESC;
解析:
sql 中 & 是且的意思 <> 是 不等於 的意思 order by 是根據指定的列對結果集進行排序,預設是對結果進行升序排序 最大值的位置: 如果是ASC升序則排最後,DESC降序則排最前