'Case BETWEEN TIMESTAMP inside Where clause MySQL
i would like to SUM netto FROM table raw_sugar WHERE the condition its depend on my var @now
When i query manually like this
SELECT SUM(`netto`) FROM `raw_sugar`
WHERE `time` BETWEEN '2022-2-7 5:00:00' AND '2022-2-7 13:00:00'
the result is correctly shown, but when i use this code
SET @now := '2022-2-7 8:00:00';
SET @pagi := '2022-2-7 5:00:00';
SET @sore := DATE_ADD(@pagi, INTERVAL + 8 hour);
SET @malam := DATE_ADD(@sore, INTERVAL + 8 hour);
SET @malam2 := DATE_ADD(@malam, INTERVAL + 8 hour);
SELECT SUM(`netto`)
FROM `raw_sugar`
WHERE (CASE
WHEN @now BETWEEN @pagi AND @sore THEN `time` BETWEEN @pagi AND @sore
WHEN @now BETWEEN @sore AND @malam THEN `time` BETWEEN @sore AND @malam
WHEN @now BETWEEN @malam AND @malam2 THEN `time` BETWEEN @malam AND @malam2
END)
it doesn't work, it shown NULL, is there any advice ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
