[英]Mysql 'greater than(>)' query always returns 0
I am working with a query where I want to display number of upcoming dates. 我正在查询一个要显示即将到来的日期数的查询。 The following query returns 0 even though there are dates greater than current date.
即使日期大于当前日期,以下查询也会返回0。 Please help me to solve this problem.
请帮我解决这个问题。
SELECT (case when b.booked_date > cast(now() as date) then sum(1) else sum(0) end) as upcoming_booked_facilities
from svk_apt_book_facilities b
where b.customer_id = 1
and b.association_id = 1
and b.is_active = 1
group by b.facility_id
You need to sum a CASE
expression to do conditional aggregation: 您需要对
CASE
表达式求和以进行条件聚合:
SELECT
facility_id,
SUM(CASE WHEN booked_date > CURDATE() THEN 1 ELSE 0 END) AS upcoming_booked_facilities
FROM svk_apt_book_facilities
WHERE
customer_id = 1 AND
association_id = 1 AND
is_active = 1
GROUP BY
facility_id;
You were trying to use the sum as the predicate of the CASE
expression, which is probably not what you want. 您试图将和用作
CASE
表达式的谓词,这可能不是您想要的。 Note that I am also selecting the facility_id
, since you are grouping by that column. 请注意,由于您要按该列分组,因此我也选择了
facility_id
。 If you instead want a conditional sum over the entire table, then don't select or group by facility. 如果您想要整个表的条件总和,则不要选择或按功能分组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.