Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 697877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:12:47+00:00 2026-05-14T03:12:47+00:00

In a MySQL query I am using the timediff/time_to_sec functions to calculate the total

  • 0

In a MySQL query I am using the timediff/time_to_sec functions to calculate the total minutes between two date-times.

For example:

2010-03-23 10:00:00
-
2010-03-23 08:00:00
= 120 minutes

What I would like to do is exclude any breaks that occur during the selected time range.

For example:

2010-03-23 10:00:00
-
2010-03-23 08:00:00
-
(break 08:55:00 to 09:10:00)
= 105 minutes

Is there a good method to do this without resorting to a long list of nested IF statements?

UPDATE1:

To clarify – I am trying to calculate how long a user takes to accomplish a given task. If they take a coffee break that time period needs to be excluded. The coffee breaks are a at fixed times.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:12:47+00:00Added an answer on May 14, 2026 at 3:12 am

    sum all your breaks that occur during the times, and then subtract to the result of the timediff/time_to_sec function

    SELECT TIME_TO_SEC(TIMEDIFF('17:00:00', '09:00:00')) -- 28800
    
    SELECT TIME_TO_SEC(TIMEDIFF('12:30:00', '12:00:00')) -- 1800
    SELECT TIME_TO_SEC(TIMEDIFF('10:30:00', '10:15:00')) -- 900
    
    -- 26100
    

    Assuming this structure :

    CREATE TABLE work_unit (
     id INT NOT NULL,
     initial_time TIME,
     final_time TIME
    )
    CREATE TABLE break (
     id INT NOT NULL,
     initial_time TIME,
     final_time TIME
    )
    INSERT work_unit VALUES (1, '09:00:00', '17:00:00')
    INSERT break VALUES (1, '10:00:00', '10:15:00')
    INSERT break VALUES (2, '12:00:00', '12:30:00')
    

    You can calculate it with next query:

    SELECT *, TIME_TO_SEC(TIMEDIFF(final_time, initial_time)) total_time
    , (SELECT SUM(
     TIME_TO_SEC(TIMEDIFF(b.final_time, b.initial_time)))
      FROM break b 
      WHERE (b.initial_time BETWEEN work_unit.initial_time AND work_unit.final_time) OR (b.final_time BETWEEN work_unit.initial_time AND work_unit.final_time)
     ) breaks 
    FROM work_unit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to pass date to mysql query using php, but i am
I am using JDBC to query a MySQL database for specific records from two
TIMEDIFF in MYSQL I am using Interchange IC (http://interchange.rtfm.info/icdocs/index.html) [query list=1 sql=SELECT TIMEDIFF(NOW(),'[sql-param time]')
i using MySQL Query for my task. And I interested using Date and time
I recently modified a MYSQL query using the keyword RLIKE however when I ran
I have tried fetching MySQL query results using mysql_fetch_row() and mysql_result() and numeric values
I am trying to run a mysql insert query using php exec with parameters.
MySQL Query works fine using MySQL workbench but produces an error when I am
I'm using GROUP_CONCAT() in a MySQL query to convert multiple rows into a single
I am using in C# MYsql .I have query that works if I run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.