2

I have table with 3 fileds id, startdate, enddate. Now I have added new field called mins as mentioned in image.

I need query to get diffrence between startdate and enddate in minutes and store in fields mysql.

image

massko
  • 589
  • 1
  • 7
  • 22
  • Please look here for answer on the same question: http://stackoverflow.com/a/40409810/6756033 – massko Nov 07 '16 at 07:51

2 Answers2

0

Use MySQL TIMESTAMPDIFF() :

UPDATE YourTable t 
set t.mins =  ABS(TIMESTAMPDIFF(MINUTE,t.startdate,t.enddate)) ;
sagi
  • 40,026
  • 6
  • 59
  • 84
0

Given you have added the minutes column later, you should update your table using the following query.

 INSERT INTO yourTable 
    SELECT 
      inn.id,
      inn.StartDate,
      inn.EndDate,
      TIMESTAMPDIFF(
        MINUTE,
        inn.StartDate,
        inn.EndDate
      ) AS diff 
    FROM
      `yourTable` inn 
      ON DUPLICATE KEY 
      UPDATE 
        id = inn.id,
        `StartDate` = inn.StartDate,
        EndDate = inn.EndDate,
        Mins = TIMESTAMPDIFF(
          MINUTE,
          inn.StartDate,
          inn.EndDate
        ) ;

Suppose this is the initial state of your table

  id  StartDate            EndDate                Mins  
------  -------------------  -------------------  --------
     1  2016-11-07 12:53:27  2016-11-30 12:53:35         0
     2  2016-11-02 12:53:41  2016-12-07 12:53:45         0
     3  2016-11-16 12:53:52  2017-02-16 12:53:56         0

Executing the above query transforms the table like this :

    id  StartDate            EndDate                Mins  
------  -------------------  -------------------  --------
     1  2016-11-07 12:53:27  2016-11-30 12:53:35     33120
     2  2016-11-02 12:53:41  2016-12-07 12:53:45     50400
     3  2016-11-16 12:53:52  2017-02-16 12:53:56    132480
Danish Bin Sofwan
  • 476
  • 1
  • 6
  • 21