Php/MySQL : Hours reported, 每小时费率

标签 php mysql

我有下表(简化):

   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/

相关文章:

php - 使用 PHP 手动解析原始多部分/表单数据数据

php - 用逗号分解字符串并从每个值中修剪潜在的空格

php - Laravel-SQLSTATE[23000] : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

mysql - 具有多个 where 和 order by 子句的慢查询

mysql - 执行 psql/mysql CLI 命令行后如何获取查询 ID?

php - 如何获得 SQL 中重复项求和的最佳项?

php - SQL、PHP : Converting Date Format

php - 将图像插入 MySQL 数据库

php - 替换字符串 PHP REGEX 中的模式

php - 在 2 个表中列出成员的所有内容(问题、答案)