Home > Software engineering >  SQL - How to subtract two timestamps from each other within an existing query
SQL - How to subtract two timestamps from each other within an existing query

Time:08-10

Basically, I'm running a massive query with a lot of joins - it is also work-related, so I cannot export the query.

However, one of my goals within the query is to run a "Total Event Time." As is, the following are in my query

,e.start_time
,e.end_time
,f.sample_data
,a.blocked_content

From inside of here, I want to see if I can calculate the different between start_time and end_time to get a Total_Time in minutes.

So basically, if the event starts at 7pm and ends at 9pm, I want 120 to be my output, for 120 minutes.

Any help or ideas?

CodePudding user response:

You can use timediff function, pls refer to the documentation

https://www.w3schools.com/sql/func_mysql_timediff.asp

CodePudding user response:

Use timediff to calculate the datetime difference, then use time_to_sec to get the elapsed time in seconds:

select time_to_sec(
                timediff(e.end_time,e.start_time)
);
  • Related