Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php/MySQL:报告小时数,小时费率_Php_Mysql - Fatal编程技术网

Php/MySQL:报告小时数,小时费率

Php/MySQL:报告小时数,小时费率,php,mysql,Php,Mysql,我有以下表格(简化): 小时表示例: 1 - 2012-03-19 - 8 这意味着id=1的用户在2012年3月19日总共工作了8小时 一个人的小时工资可以随时间变化,因此我有第二张表: 小时费率表示例 1 - 2011-12-01 - 20 1 - 2011-12-20 - 25 因此,对于id=1的用户,我们将2011-12-01的小时费率设置为20美元。 我们将他2011年12月20日的小时工资改为25美元 我想要的是,计算在给定的时间段(例如2012-01-01->2012-02-

我有以下表格(简化):

小时表示例:

1 - 2012-03-19 - 8
这意味着id=1的用户在2012年3月19日总共工作了8小时

一个人的小时工资可以随时间变化,因此我有第二张表:

小时费率表示例

1 - 2011-12-01 - 20
1 - 2011-12-20 - 25
因此,对于id=1的用户,我们将2011-12-01的小时费率设置为20美元。 我们将他2011年12月20日的小时工资改为25美元

我想要的是,计算在给定的时间段(例如2012-01-01->2012-02-01),我必须为给定的用户(例如id=1)支付多少费用

我可以简单地计算这一方面吗? 如果没有,如何以有效的方式进行

我可以简单地计算这一方面吗

是的,这是SQL

select sum(outer_h.hours * 
  (select inner_hr.hourly_rate 
    from hour_rates inner_hr
    where inner_hr.user_id = outer_h.user_id
    and inner_hr.date >= outer_h.date
    order by inner_hr.date asc
    limit 1)
) as pay
  from hours outer_h
  where outer_h.user_id = :user_id --here you will set the user id parameter
  and outer_h.date between STR_TO_DATE('01,1,2012','%d,%m,%Y') and STR_TO_DATE('01,1,2011','%d,%m,%Y')
我可以简单地计算这一方面吗

是的,这是SQL

select sum(outer_h.hours * 
  (select inner_hr.hourly_rate 
    from hour_rates inner_hr
    where inner_hr.user_id = outer_h.user_id
    and inner_hr.date >= outer_h.date
    order by inner_hr.date asc
    limit 1)
) as pay
  from hours outer_h
  where outer_h.user_id = :user_id --here you will set the user id parameter
  and outer_h.date between STR_TO_DATE('01,1,2012','%d,%m,%Y') and STR_TO_DATE('01,1,2011','%d,%m,%Y')

在小时费率表中,您应该有两个日期:开始日期和结束日期。这意味着从开始日期到结束日期,员工的工资为每小时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')

在小时费率表中,您应该有两个日期:开始日期和结束日期。这意味着从开始日期到结束日期,员工的工资为每小时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')

编辑:那么,对不起,你需要两张桌子做什么?它们是完全相等的,如果每天都有自己的小时费率,您只需将其存储如下:

小时数:用户id、日期、小时数、小时费率

e、 g.1 | 2012-03-19 | 8 | 25

你用

 SELECT user_id, date, hours, hourly_rate FROM hours WHERE (user_id=$var_of_user_id AND date ...)
然后乘以每行小时数*小时费率,并将其添加到$sum,例如

while {...

$sum=$sum+($row['hours']*$row['hourly_rate']);

}

每小时的费用取决于什么?可能您不希望在小时费率表中包含字段日期

如果每个作业的小时工资率不同,则只希望有一个表包含用户id、开始日期、结束日期(或工作小时数)和小时工资率

如果小时费率仅取决于用户id,则需要有两个表:

小时数:用户id、开始日期、结束日期(或工作小时数)

小时费率:用户id,小时费率

并在用户id上加入表。如果您已经有一个表用户,那么在第二种情况下,您也可以在那里存储小时费率


然后使用php简单地将工作小时数乘以每小时费率,其中用户id。。。等等。

编辑:对不起,你需要两张桌子做什么?它们是完全相等的,如果每天都有自己的小时费率,您只需将其存储如下:

小时数:用户id、日期、小时数、小时费率

e、 g.1 | 2012-03-19 | 8 | 25

你用

 SELECT user_id, date, hours, hourly_rate FROM hours WHERE (user_id=$var_of_user_id AND date ...)
然后乘以每行小时数*小时费率,并将其添加到$sum,例如

while {...

$sum=$sum+($row['hours']*$row['hourly_rate']);

}

每小时的费用取决于什么?可能您不希望在小时费率表中包含字段日期

如果每个作业的小时工资率不同,则只希望有一个表包含用户id、开始日期、结束日期(或工作小时数)和小时工资率

如果小时费率仅取决于用户id,则需要有两个表:

小时数:用户id、开始日期、结束日期(或工作小时数)

小时费率:用户id,小时费率

并在用户id上加入表。如果您已经有一个表用户,那么在第二种情况下,您也可以在那里存储小时费率


然后使用php简单地将工作小时数乘以每小时费率,其中用户id。。。等等。

此查询应该可以:

SELECT SUM( h.hours*COALESCE(hr.hourly_rate,0) ) AS salary
FROM hours h
LEFT JOIN hour_rates hr
ON h.user_id = hr.user_id 
AND  hr.date = ( SELECT MAX(hd.date) FROM hour_rates hd
                 WHERE hd.user_id = h.user_id AND hd.date <= h.date )
WHERE h.user_id = 1
AND h.date BETWEEN '2012-01-01' AND '2012-04-01'
选择总和(小时*合并(小时小时费率,0))作为工资
从小时到小时
左连接小时/小时费率
在h.user\u id=hr.user\u id上
和hr.date=(从hour_rates hd中选择MAX(hd.date)

其中hd.user\u id=h.user\u id和hd.date此查询应该可以工作:

SELECT SUM( h.hours*COALESCE(hr.hourly_rate,0) ) AS salary
FROM hours h
LEFT JOIN hour_rates hr
ON h.user_id = hr.user_id 
AND  hr.date = ( SELECT MAX(hd.date) FROM hour_rates hd
                 WHERE hd.user_id = h.user_id AND hd.date <= h.date )
WHERE h.user_id = 1
AND h.date BETWEEN '2012-01-01' AND '2012-04-01'
选择总和(小时*合并(小时小时费率,0))作为工资
从小时到小时
左连接小时/小时费率
在h.user\u id=hr.user\u id上
和hr.date=(从hour_rates hd中选择MAX(hd.date)

如果hd.user\u id=h.user\u id和hd.date,为什么要在mysql端这样做?如果不需要在mysql查询中使用结果,那么应该在代码中进行计算;否则使用存储过程/函数我建议使用稍微不同的方法,我会在您创建一个条目,它会简化一切。@Bengrifiths这是一个很好的选择。您为什么要在mysql端这样做?如果您不需要在mysql查询中使用结果,您应该在代码中进行计算;否则使用存储过程/函数我建议使用稍微不同的方法,我会添加每小时rat的ID当你在
hours
表中输入e时,它会简化一切。@Bengrifiths这可能是一个很好的选择。看起来像是一个非常漂亮的查询。我是sospicius About的唯一一点是,你使用了这个连接条件,h.date=hr.date,但hr.date意味着从这个日期起,雇员现在每小时有这个价格。假设hr.date is=2012-01-01您想计算2012-01-02日雇佣的工资金额,但是and条件失败,查询没有返回结果。我不确定,应该测试查询……嗯……您完全正确,我没有足够仔细,只是浏览了一下问题并写了一个快速答案。让我来解决这个问题。看起来是一个非常漂亮的查询。我是sospicius About的唯一一点是,您使用此联接条件,h.date=hr.date,但hr.date意味着从该日期起,员工现在每小时有此价格。假设hr.date=2012-01-01,您希望计算2012-01-02日员工的工资金额,但and条件失败,并且没有结果“我不确定,这个查询应该进行测试……嗯……你说得完全正确,我没有足够仔细,只是浏览了一下问题,然后写了一个快速的答案。让我来解决这个问题。一个人的小时工资可以随着时间的变化而变化,对于相同的任务和相同的项目。@Tamas Pap:但小时工资是最便宜的。”