MySQL 具有多个 BETWEEN 的查询始终为 NULL

MySQL query with multiple BETWEEN is always NULL

也许我在这里遗漏了明显的东西,但为什么这个查询总是 NULL?

SELECT ROUND(AVG(`viewer_count`),0) AS avg_viewer FROM `table` 
WHERE 
(id BETWEEN 1 AND 8) 
AND 
(id BETWEEN 26 AND 32)

像这样求多个范围的平均值不行吗?

SELECT ROUND(AVG(CASE WHEN id BETWEEN 1 AND 8   THEN `viewer_count` END),0) AS avg_viewer1_8
     , ROUND(AVG(CASE WHEN id BETWEEN 26 AND 32 THEN `viewer_count` END),0) AS avg_viewer26_32
FROM `table` 

由于 id 不可能介于 1 和 8 之间,同时又介于 26 和 32 之间,因此查询返回 NULL,因为没有匹配的行。如果您按照评论中建议的 squemeamish 使用 OR,您将获得两个范围内的平均值。