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 8655865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:08:35+00:00 2026-06-12T15:08:35+00:00

I’m dealing with (for me) huge SQL problem. I have table called timeshifts which

  • 0

I’m dealing with (for me) huge SQL problem. I have table called “timeshifts” which as 4 columns:
id, name, from and to. Last two type is TIME.

Now: I have three timeshifts:
06:00 – 14:00
14:00 – 22:00
22:00 – 06:00

Now, I have to find correct shift for current time (which works like a charm), but I can’t find a proper records that belong to current shift. IE. table called user_times contains fields with separate date and time values. According to current timeshift I have to find records that belongs to the timeshift. There is no problem for first two shifts, but third one passes midnight and that is my problem.

Here is the query that finds proper shift, event through midnight:

SELECT 
  FLOOR(SUM(time_sum) / 60) AS th,
  MOD(SUM(time_sum), 60) AS tm,
  t.* 
FROM
  ur_user_times ut,
  ur_timeshifts t 
WHERE CURTIME() >= IF (
    t.timeshift_to < t.timeshift_from,
    0,
    ADDTIME(t.timeshift_from, "00:15")
  ) 
  AND CURTIME() <= ADDTIME(
    IF (
      t.timeshift_to < t.timeshift_from,
      ADDTIME(t.timeshift_from, "10:00:00"),
      t.timeshift_to
    ),
    "00:15"
  ) 
  AND ut.`date` BETWEEN CURDATE() - INTERVAL 1 DAY AND CURDATE()
  AND ut.`start` BETWEEN ADDTIME(t.`timeshift_from`, "-10:00:00") AND ADDTIME(t.`timeshift_to`, "-10:00:00")
  AND ut.user_id = 40 

ut.start is time value that interests us.

  • 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-06-12T15:08:37+00:00Added an answer on June 12, 2026 at 3:08 pm

    Try this:

    select *
    from timeshifts ts,
    usertimes ut
    where 
    (ts.from < ts.to and ut.start >= ts.from and ut.start <= ts.to) OR
    (ts.from > ts.to and (ut.start >= ts.from or ut.start <= ts.to))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I've tracked down a weird MySQL problem to the two different ways I was
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.