我有一個簡單的表格,並且我想將它們按id_room
分組,並且只有在條件爲真時才選擇值。問題是,即使有一行右列日期列year_month
,該條件始終爲假。MySQL - 組中的條件選擇
下面是模式:
CREATE TABLE tbl_account_room (
`id` int,
`year_month` date,
`value` int,
`id_room` int
);
INSERT INTO tbl_account_room
(`id`, `year_month`, `value`, `id_room`)
VALUES
(1, '2016-08-01', 1, 300),
(2, '2016-09-01', 2, 300),
(3, '2016-10-01', 3, 300);
這裏查詢:
SELECT
(case when '2016-10-01' = ar.year_month then ar.value else 0 end) as total
FROM tbl_account_room AS ar
WHERE ar.year_month >= "2016-08-01"
AND ar.year_month <= "2016-11-01"
and ar.id_room = '300'
GROUP BY ar.id_room
LIMIT 10
這裏是SQL Fiddle
在total
列我越來越0,我想獲得價值3,因爲year_month
是2016-10-01
。爲什麼會發生?