leetcode-SQL 620. 有趣的電影(難度:簡單)--奇數偶數的區別

某城市開了一家新的電影院,吸引了很多人過來看電影。該電影院特別注意用戶體驗,專門有個 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
 

方法一:
select id, movie, description, rating
from cinema
where round(id/2) > (id/2)
and description <> "boring"
order by rating desc;

方法二:
select id, movie, description, rating
from cinema
where mod(id,2) = 1
and description != "boring"
order by rating desc;

方法三:
select id, movie, description, rating
from cinema
where id%2 = 1
and description != "boring"
order by rating desc;

SQL中“<>” 和 “!=” 都表示“不等於”,但推薦用<>,因爲ANSI標準中是用<>

使用"%"取餘符號和mod()結果一樣,不過mod()效率更高。

mod函數百是一個求餘函數,其格式爲: mod(nExp1,nExp2),即是兩個數值表達式作除法運算(nExp1/nExp2)後的餘數。

示例:

MOD(3, 2) 等於 1

MOD(-3, 2) 等於1

MOD(3, -2) 等於-1

MOD(-3, -2) 等於-1

MOD(-3, 0) 等於-3

MOD(3, 0) 等於3

MOD(2, 0) 等於2

MOD(4, 3) 等於1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章