Mysql 如果字段值大于 1 且不小于零,则计算 datediff 总行数
Mysql Count datediff total rows if field value more than 1 and not less than zero
创建post的目的是,我找不到符合我现在遇到的案例的案例,我想从截止日期开始提取金额大于 1 而不是负或等于零
如果它已经存在post,我很抱歉,因为它没有找到合适案例的例子。我只需要一个符合我案例的例子
我的数据库
id userid duedate status
1 lexter 2016-01-27 not yet paid
2 jemmy 2016-02-01 not yet paid
3 zaya 2016-03-02 already paid
4 randy 2016-08-09 not yet paid
5 max 2016-03-08 not yet paid
6 neo 2016-03-14 already paid
7 nemo 2016-01-09 not yet paid
这个mysql查询
SELECT duedate,
current_date() as datenow,
count(datediff(current_date(), duedate) as late_payment) as late_payment
FROM
mydb
WHERE
status='not yet paid'
虽然他小于零或大于零,但我的查询只计算所有记录,我希望只计算大于零且尚未支付的记录。
这称为条件计数,您可以通过 sum() 或 count() 来完成。您需要将 case 语句或 if() 放在 group by 函数中。我将展示 count():
的示例
SELECT duedate,
current_date() as datenow,
count(case when datediff(current_date(), duedate) >0 then 1 else null end) as late_payment_count
FROM
mydb
WHERE
status='not yet paid'
group by duedate, current_date()
你可以简化条件,因为datediff(current_date(), duedate) >0
其实和current_date() > duedate
是一样的。
如果您只对延迟付款的次数感兴趣,那么您可以进一步简化您的查询
SELECT duedate,
current_date() as datenow,
count(*) as late_payment_count
FROM
mydb
WHERE
status='not yet paid' and current_date() > duedate
group by duedate, current_date()
创建post的目的是,我找不到符合我现在遇到的案例的案例,我想从截止日期开始提取金额大于 1 而不是负或等于零
如果它已经存在post,我很抱歉,因为它没有找到合适案例的例子。我只需要一个符合我案例的例子
我的数据库
id userid duedate status
1 lexter 2016-01-27 not yet paid
2 jemmy 2016-02-01 not yet paid
3 zaya 2016-03-02 already paid
4 randy 2016-08-09 not yet paid
5 max 2016-03-08 not yet paid
6 neo 2016-03-14 already paid
7 nemo 2016-01-09 not yet paid
这个mysql查询
SELECT duedate,
current_date() as datenow,
count(datediff(current_date(), duedate) as late_payment) as late_payment
FROM
mydb
WHERE
status='not yet paid'
虽然他小于零或大于零,但我的查询只计算所有记录,我希望只计算大于零且尚未支付的记录。
这称为条件计数,您可以通过 sum() 或 count() 来完成。您需要将 case 语句或 if() 放在 group by 函数中。我将展示 count():
的示例SELECT duedate,
current_date() as datenow,
count(case when datediff(current_date(), duedate) >0 then 1 else null end) as late_payment_count
FROM
mydb
WHERE
status='not yet paid'
group by duedate, current_date()
你可以简化条件,因为datediff(current_date(), duedate) >0
其实和current_date() > duedate
是一样的。
如果您只对延迟付款的次数感兴趣,那么您可以进一步简化您的查询
SELECT duedate,
current_date() as datenow,
count(*) as late_payment_count
FROM
mydb
WHERE
status='not yet paid' and current_date() > duedate
group by duedate, current_date()