我有两个表: t_address -> 使用用户地址 t_billing -> 包含 t_address 用户的账单信息
它们由 t_billing.bil_addressref = t_address.adr_ref 链接
在账单表中,我有一个字段 bil_date -> 签发/发送账单的日期 bil_paydate -> 账单支付日期
我不想确定“最差付款人”,即需要平均最长时间支付账单的人。
例如:
没有。 -- bil_date -- bil_paydate -- adr_ref
1 -- 2017-01-01 -- 2017-01-10 - 1
2 -- 2017-02-01 -- 2017-02-10 - 1
3 -- 2017-03-01 -- 2017-03-05 - 1
4 -- 2017-04-01 -- 2017-04-05 - 1
5 -- 2017-01-01 -- 2017-01-30 - 2
6 -- 2017-03-01 -- 2017-03-30 - 2
...
所以
对于 ADR 2,平均支付时间为 29 天
对于 ADR 1,平均支付时间为 6.5 天
我想要最差付款人的前 5 个结果,从最差到不太差排序。
我尝试了几个问题和想法,但没有找到正确的想法:/
有什么建议吗?谢谢,斯蒂芬。
最佳答案
你可以使用 join 、 group by 和 limit
select adr_ref, sum( datediff(bil_paydate,bil_date ))/ count(*) my_result
from t_address
inner join t_billing on t_billing.bil_addressref = t_address.adr_ref
group by adr_ref
order by my_result desc
limit 5
关于PHP/Mysql -> 平均每个用户的计费/支付日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45512179/