Skip to content
Advertisement

Get results from SELECT then UPDATE it into the database MySQL and PHP

I want to get the time average by using this command in PHP :

SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(PunchInTime))) as Average from AverageOnTime;

so is this code below correct to combine update and avg:

UPDATE table_3_2020 SET pastTime = SEC_TO_TIME(AVG(TIME_TO_SEC(currentTime))) WHERE id = '1'

I have two requests:

  1. I want to UPDATE another column with the average, so how can I do that?
  2. When I use the above SELECT I got the HH:MM but I just want the minutes only without the hours.

Advertisement

Answer

Try this

UPDATE table_3_2020 SET pastTime = (SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(PunchInTime))) as Average from AverageOnTime) WHERE id = '1';
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement