我有下表(简化):
hours hour_rates
- user_id - user_id
- date - date
- hours - hourly_rate
小时表示例:
1 - 2012-03-19 - 8
这意味着 id=1 的用户在 2012-03-19 总共工作了 8 小时。
一个人的小时费率会随时间变化,所以我有第二张表:
hour_rates 表示例
1 - 2011-12-01 - 20
1 - 2011-12-20 - 25
因此对于 id=1 的用户,我们在 2011 年 12 月 1 日将每小时费率设置为 20 美元。 我们在 2011 年 12 月 20 日将他的时薪更改为 25 美元。
我想要的是计算在给定时间段(例如 2012-01-01 -> 2012-02-01)内我必须为给定用户(例如 id=1)支付多少费用。
我可以简单地计算这个mysql端吗? 如果没有,如何以有效的方式做到这一点?
最佳答案
在 hour_rates 表中你应该有两个日期:start_date 和 end_date 这意味着从 start_date 到 end_date 雇主每小时支付 x$。
然后使用 bpgergo 提出的相同查询修改如下:
select sum(h.hours * hr.hourly_rate) as pay
from hours h, hour_rates hr
where h.user_id = :user_id --here you will set the user id parameter
and h.user_id = hr.user_id and (h.date BETWEEN hr.start_date AND hr.end_date
and h.date between STR_TO_DATE('01,1,2012','%d,%m,%Y') and STR_TO_DATE('01,1,2011','%d,%m,%Y')
关于Php/MySQL : Hours reported, 每小时费率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9769486/