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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:25:29+00:00 2026-05-29T11:25:29+00:00

I am having a bit of an issue with nested CASE in MySQL. The

  • 0

I am having a bit of an issue with nested CASE in MySQL.

The rest_opening_hours table is as follows:

rest_opening_hours (
  restid int,
  day_of_week int,
  hours_open time,
  hours_close time,
)

I am then trying to perform a query. The reason for the WHEN DAYOFWEEK( NOW()) = 1 bit is that I notice that if you try to take away on a Sunday (day 1) it just returns 1, which causes issues.

SELECT h.hours_close
FROM restaurants s
INNER JOIN rest_opening_hours h
ON s.id = h.restid
WHERE 
CASE 
    WHEN h.hours_close > h.hours_open
    THEN h.day_of_week = DAYOFWEEK(NOW()) 
ELSE 
    CASE 
        WHEN DAYOFWEEK(NOW()) = 1
        THEN h.day_of_week = 7
    ELSE
        h.day_of_week = DAYOFWEEK(NOW() - 1) 
    END 
END 
AND s.id = '2'
LIMIT 0 , 30

Here is some data from the rest_opening_hours table:

INSERT INTO `rest_opening_hours` (`restid`, `day_of_week`, `hours_open`, `hours_close`) VALUES
(2, 1, '17:00:00', '23:00:00'),
(2, 7, '17:00:00', '06:00:00'),
(2, 6, '17:00:00', '00:00:00'),
(2, 5, '17:00:00', '01:00:00'),
(2, 4, '17:00:00', '02:00:00'),
(2, 3, '03:00:00', '23:00:00'),
(2, 2, '17:00:00', '04:00:00');

My only problem is, that my query is returning multiple records, and I don’t really see why. The query returns (on a Sunday – day 1) the results for Sunday (day 1) and also for Saturday (day 7) not just day 7 as I had intended in my query!?

I would expect the result “06:00” to be returned, as today it is Sunday (day 1) and following the logic, when DAYOFWEEK(NOW()) = 1 then we select where h.days_of_week = 7 (ie, Yesterday’s opening hours) as we previously deduced that h.hours_close < h.hours_open. However, I am getting two results: “06:00” and “23:00” which makes no sense to me, it’s almost as if MySQL is ignoring the second case and just returning both!??!?!

Can anyone shed any light on this, as I have spent ages playing around with this and I don’t seem to be getting anywhere.

Thanks in advance!

Ryan

Amended Query

SELECT h.hours_close
FROM restaurants s
INNER JOIN rest_opening_hours h ON s.id = h.restid
WHERE 
CASE 
    WHEN h.hours_close > h.hours_open
    THEN h.day_of_week = DAYOFWEEK( NOW( ) ) 
    ELSE h.day_of_week = DAYOFWEEK( DATE_SUB( NOW( ) , INTERVAL 1 DAY ) ) 
END 
AND s.id =  '2'

It appears that my issue is that my query is selecting both the day before (7) and the day in question (1) because one is h.hours_close > h.hours_open and the other is h.hours_close < h.hours_open – any ides on how I could isolate this to just one result, perhaps a restructure of sorts so that I am only looking at the one for the day before IF the closing time is less than the opening time (that is, it closes the next day).

In Pseudo-code:

if (closingtime < openingtime) {
return closing time from yesterday
} else {
return closing time from today
}

it is so simple – why is it causing me so many damn problems!?!?!?

  • 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-29T11:25:30+00:00Added an answer on May 29, 2026 at 11:25 am

    God bless pseudo code. That was all I needed 🙂 This is what you want:

    select * from t1
    join (
      select restid, day_of_week,
        if(hours_close < hours_open,
          if(day_of_week = 1, 7, day_of_week - 1),
          day_of_week
        ) as NewDayOfWeek
      from t1
    ) as S
    on s.restid = t1.restid and s.day_of_week = t1.day_of_week
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of an issue uploading files to a database. The table
hello fellow java developers. I'm having a bit of an issue here. I have
Ok. I'm having an issue with the following bit of code: StreamReader arrComputer =
Having a bit of an issue with wordpress URLs, it seems to automatically add
Im having a bit of an issue with public -v- private variables and scope.
I am having a bit of an issue right now. Is there any simple
Well I'm having a bit of an issue, I have an application that uses
I'm having a bit of an issue with interaction beween my app and other
I am having a bit of an issue with a click event not firing
I am having a bit of an issue with getting tumblr working within a

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.