某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 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 | +---------+-----------+--------------+-----------+
select * from cinema where mod(id,2)=1 and description<>'boring' order by rating desc
延伸:mysql中判断奇数偶数(注意效率,千万级数据时索引使用情况等)
-- 按位与 select * from cinema WHERE id&1; -- id先除以2然后乘2 如果与原来的相等就是偶数 select * from cinema WHERE id=(id>>1)<<1; -- 正则匹配最后一位 select * from cinema WHERE id regexp '[13579]$'; select * from cinema WHERE id regexp '[02468]$'; -- id计算 select * from cinema WHERE id%2 = 1; select * from cinema WHERE id%2 = 0; -- 与上面的一样 select * from cinema WHERE mod(id, 2) = 1; select * from cinema WHERE mod(id, 2) = 0; -- -1的奇数次方和偶数次方 select * from cinema WHERE POWER(-1, id) = -1; select * from cinema WHERE POWER(-1, id) = 1;