if语句在mysql查询中

时间:2014-05-07 00:07:37

标签: php mysql sql

您好我尝试在mysql查询中创建一个if语句,但我不知道我哪里出错了。 我试图做的是,如果该列值超过8小时,则从列值中扣除8。这是'到目前为止我做了什么

$sql="UPDATE timekeeping SET actualend = timestamp(NOW()), logoutdate = date(NOW()),
totalhours = ((SELECT sum(time_to_sec(timediff(actualend,actualstart))/3600) AS
'totalhours') - 1.50), 
excesstime = (if (totalhours > 8) 
   {
excesstime = (totalhours - 8);
   }
)  WHERE actualend IS NULL and fullname = '$loginuser[fullname]'";

所以,如果我展示一个类似下面的表

时间,超时,总时数,超时时间

9:00:00 | 18:00:00 | 9 | 1< ------这应该是答案(1)

很抱歉这个,但我在mysql和php中都是新手。

1 个答案:

答案 0 :(得分:3)

您尝试在查询中使用流控制语句,其中您实际需要流控制功能。请改用if()功能。

试试这个:

$sql="UPDATE timekeeping SET actualend = timestamp(NOW()), logoutdate = date(NOW()),
totalhours = ((SELECT sum(time_to_sec(timediff(actualend,actualstart))/3600) AS
'totalhours') - 1.50), 
excesstime = if(totalhours > 8,totalhours-8,0)
  WHERE actualend IS NULL and fullname = '$loginuser[fullname]'";

流控制语句用于存储过程,触发器等,并且在单行查询中无效。

MySQL参考here