dqsk4643 2014-05-07 00:07
浏览 15
已采纳

if语句在mysql查询中

Hi I tried creating an if statement inside a mysql query but I dont know where I went wrong. What I tried to do is to deduct 8 from a column value if that column value is more than 8 hours. Here' what I have done so far

$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]'";

So if I show a table like the one below

time in, timeout, totalhours, excesstime respetively

9:00:00 | 18:00:00 | 9 | 1 <------this should be the answer(1)

Sorry abut this but I'm so new in mysql as well as in php.

  • 写回答

1条回答 默认 最新

  • dongtuo1482 2014-05-07 00:13
    关注

    You're attempting to use a flow control statement in your query, where you actually need a flow control function. Use the if() function instead.

    Try this:

    $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]'";
    

    The flow control statements are used in stored procedures, triggers, etc. and aren't valid in single-line queries.

    MySQL reference here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页