0

I'm trying to convert an excel scoring program and this one formula is driving me nuts. so this is the excel formula =IF(AD4<AE4,AE4-AD4,0) and I have been stumped for a little bit. I have looked at, and tried a few sample formulas for both PHP & MySQL, but I keep receiving syntax errors, unexpected if or else in the code.

So what the formula is checking to see if the elapsed time is under a set time, and if so, calculates the difference between the 2 fields and displays the outcome in time format hh:mm:ss.

This is the latest SQL query I tried:

Select *
       if (`Under Time`<`Speed Fault`,0) 
       else { (`Time Elapsed`-`Speed Fault`)} As Under Time 
from time_db
Marko Ivkovic
  • 1,262
  • 1
  • 11
  • 14
dmcomp
  • 3
  • 3
  • You can use the SQL `IF()` to accomplish the same thing: `SELECT *, IF(\`Under Time\` < \`Speed Fault\`, \`Time Elapsed\`-\`Speed Fault\`, 0) AS \`Under Time\` FROM time_db`. – kmoser Jul 10 '21 at 00:33

2 Answers2

0

In sql , you can use case :

Select *,case when (UnderTime<SpeedFault) 
              then 0 
              else TimeElapsed - SpeedFault end As UnderTime 
from time_db
eshirvana
  • 23,227
  • 3
  • 22
  • 38
0
SELECT *,
       GREATEST(`Time Elapsed`-`Speed Fault`, 0) AS `Under Time`
FROM time_db;
Akina
  • 39,301
  • 5
  • 14
  • 25