What is wrong with this MySQL statement?

Posted on 2011-09-17
Last Modified: 2012-05-12
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

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

it computes the time difference correctly
Question by:ToreJohn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 60

Expert Comment

by:Kevin Cross
ID: 36554322
Hi. You said it does not throw an error; therefore, how are you judging that it did not compute?
LVL 60

Expert Comment

by:Kevin Cross
ID: 36554337
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


Author Comment

ID: 36554366
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)
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

LVL 60

Expert Comment

by:Kevin Cross
ID: 36554372
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.

Author Comment

ID: 36554431
Yes and Thanks.
I used datetime as the format for the output. Changing the format did the trick
LVL 60

Accepted Solution

Kevin Cross earned 500 total points
ID: 36554441
You are welcome!

Author Closing Comment

ID: 36554450
It was fine. He understood that what I had done was correct and pointed me in the right direction.

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Creating and Managing Databases with phpMyAdmin in cPanel.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question