难度:简单
编写一个 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 |
+---------+-----------+--------------+-----------+
解答:
select *
from cinema
where id%2<>0 and description<>'boring'
order by rating desc
官方题解里使用了mod(id, 2) = 1
也可以
小tips:在sql语句中,不等于的表达尽量写为<>
而不是!=