[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

What is wrong with this MySQL statement?

The statement below does not give an error code, but it does not compute the TIMEDIFF:

"UPDATE Stop_times, Start_times
SET Stop_times.Project_Charge_Time=TIMEDIFF(Stop_times.STOPid,Start_times.STARTid)
WHERE Start_times.PROJECTid = Stop_times.PROJECTid
AND
Start_times.EMPLOYid=Stop_times.EMPLOYid"

but if you delete the update and modify it to a select statement like '/**UPDATE Stop_times, Start_times
SET Stop_times.Project_Charge_Time=/**/  SELECT TIMEDIFF(Stop_times.STOPid,Start_times.STARTid)
FROM Stop_times, Start_times
WHERE Start_times.PROJECTid = Stop_times.PROJECTid
AND
Start_times.EMPLOYid=Stop_times.EMPLOYid

it computes the time difference correctly
0
ToreJohn
Asked:
ToreJohn
  • 4
  • 3
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
Hi. You said it does not throw an error; therefore, how are you judging that it did not compute?
0
 
Kevin CrossChief Technology OfficerCommented:
The syntax you have looks fine, so suspect something else. For example, by naming convention, I would not expect the actual time values to be in a column STARTid or STOPid. When I see that I think AUTO_INCREMENT or INT fields that are keys to a table. Try the below syntax ensuring that the columns in TIMEDIFF are indeed DATETIME columns. Since you said this works in SELECT, that may be the STARTid and STOPid, but may be worth double checking.

UPDATE Stop_times stp
JOIN Start_times strt 
    ON strt.PROJECTid = stp.PROJECTid
   AND strt.EMPLOYid = stp.EMPLOYid
SET stp.Project_Charge_Time = TIMEDIFF(stp.STOPdatetime, strt.STARTdatetime)
;

Open in new window

0
 
ToreJohnAuthor Commented:
I can see that it does not work because stp.Project_Charge_Time is 0000-00-00 00:00:00 also after running the query. Your alternative code gives the same result. (When I modified stp.STOPdatetime to STOPid and the same for STARTid)
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
Kevin CrossChief Technology OfficerCommented:
Wait. Project_Charge_Time is a datetime?
Remember TIMEDIFF is given you a difference in time values. It is not resulting in a datetime value. Please show the table structure(s) and some sample data with what you expect the final results to be.
0
 
ToreJohnAuthor Commented:
Yes and Thanks.
I used datetime as the format for the output. Changing the format did the trick
0
 
Kevin CrossChief Technology OfficerCommented:
You are welcome!
0
 
ToreJohnAuthor Commented:
It was fine. He understood that what I had done was correct and pointed me in the right direction.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now