0
也許我在這裏很明顯缺少這個,但爲什麼這個查詢總是NULL?與多個BETWEEN MySQL查詢總是NULL
SELECT ROUND(AVG(`viewer_count`),0) AS avg_viewer FROM `table`
WHERE
(id BETWEEN 1 AND 8)
AND
(id BETWEEN 26 AND 32)
是不是有可能得到這樣的多個範圍的平均值?
也許我在這裏很明顯缺少這個,但爲什麼這個查詢總是NULL?與多個BETWEEN MySQL查詢總是NULL
SELECT ROUND(AVG(`viewer_count`),0) AS avg_viewer FROM `table`
WHERE
(id BETWEEN 1 AND 8)
AND
(id BETWEEN 26 AND 32)
是不是有可能得到這樣的多個範圍的平均值?
既然是不可能的id
爲1和8之間,在26和32之間的同時,該查詢返回NULL作爲沒有行被匹配。如果您使用OR
作爲註釋中建議的squemeamish,您將獲得兩個範圍內的平均值。
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`
試試'OR',而不是'AND' –
數字如何可以介於1和8之間以及介於26和32之間?這在邏輯上是不可能的,夥計。 – Paulo