MySQL中的减号操作

时间:2017-11-07 11:56:31

标签: php mysql

到目前为止,我正在尝试做一个减号操作,但是我有一个我无法弄清楚的问题。

SELECT freedays_id,freedays,us.username, year,
       TIME_FORMAT(SEC_TO_TIME(freedays*86400), '%H:%i') AS Total freeydays,
       TIME_FORMAT(SUM(TIMEDIFF(time_till,time_from)), '%H:%i') AS Used freeydays(hours),
       TIME_FORMAT(SUM( TIMEDIFF( time_till, time_from )) - SEC_TO_TIME( freedays*86400 ) , '%H:%i' ) AS remaining freedays(hours)
       FROM " . TBL_freedays. " AS vr
       JOIN " . TBL_users. " AS us ON us.id = vr.id
       LEFT JOIN " . TBL_hours. " AS ur ON ur.id= vr.id
            WHERE
            ur.project_id = 10
            AND
            us.id= vr.id
            AND
            jaar = YEAR(CURDATE())
            AND
            YEAR(datum) = YEAR(CURDATE())
            GROUP BY vr.userid

如果我运行此查询,结果将为:

  1. 用户名=员工1
  2. 年= 2017
  3. Freedays = 19
  4. 总自由日= 456:00
  5. 使用freeydays(小时)= 06:00
  6. 剩余的时间(小时)= -450:00
  7. 但剩余的免费日(小时)= -450:00,我希望它是450:00

    TIME_FORMAT(SUM( TIMEDIFF( time_till, time_from )) - SEC_TO_TIME( freedays*86400 ) , '%H:%i' ) AS remaining freedays(hours)
    

    这是我的MySQL我试图改变查询,但它对我不起作用,所以如果有人可以提供帮助那就太棒了!

1 个答案:

答案 0 :(得分:0)

如果总自由时间= 456:00且使用的时间(小时)= 06:00

已用过的时间中减去自由时间时,结果将为-450:00。

我不明白你在'转动查询'时的意思,但当我TIME_FORMAT(SEC_TO_TIME( freedays*86400 ) - SUM( TIMEDIFF( time_till, time_from )), '%H:%i' ) AS remaining freedays(hours)时,我得到了正确的答案