Reputation: 49
I have one-to-many relation ship:
branches:
branch_working_hours:
What I am trying to achieve is to set a new column "intrvl" with correct open or close, consdering:
select `branches`.*,
CASE
WHEN
((CURRENT_TIME() BETWEEN branch_working_hours.from1 AND branch_working_hours.to1 )
OR (CURRENT_TIME() BETWEEN branch_working_hours.from2 AND branch_working_hours.to2)
OR branch_working_hours.is_24_hours_open= 1)
AND EXISTS (
SELECT * FROM branch_working_hours
WHERE branch_working_hours.branch_id = branches.id AND branch_working_hours.day LIKE UCASE(DAYNAME(CURRENT_DATE()))
)
THEN 'OPEN'
ELSE
CASE
WHEN
((branch_working_hours.to1 < branch_working_hours.from1 AND CURRENT_TIME() > branch_working_hours.from1)
OR (branch_working_hours.to2 < branch_working_hours.from2 AND CURRENT_TIME() > branch_working_hours.from2 ))
AND EXISTS (
SELECT * FROM branch_working_hours
WHERE branch_working_hours.branch_id = branches.id AND branch_working_hours.day LIKE UCASE(DAYNAME(CURRENT_DATE()))
)
THEN 'OPEN'
ELSE
CASE
WHEN
((branch_working_hours.to1 < branch_working_hours.from1 AND CURRENT_TIME() BETWEEN '00:00:00' AND branch_working_hours.to1)
OR (branch_working_hours.to2 < branch_working_hours.from2 AND CURRENT_TIME() BETWEEN '00:00:00' AND branch_working_hours.to2))
AND EXISTS (
SELECT * FROM branch_working_hours
WHERE branch_working_hours.branch_id = branches.id AND branch_working_hours.day LIKE UCASE(DAYNAME(SUBDATE(CURRENT_DATE(), 1)))
)
THEN 'OPEN'
ELSE 'CLOSED'
end
end
end AS intrvl
from `branches`
inner join `branch_working_hours` on `branches`.`id` = `branch_working_hours`.`branch_id`
group by `branches`.`id`
having distance < ?
order by `distance` asc
but this code most of the time returns wrong intrvl unless I add
where branch_working_hours
.day
LIKE UCASE(DAYNAME(CURRENT_DATE()))
but some times I need to check the yesterdays to get the current value
Upvotes: 0
Views: 46
Reputation: 49
Found the solution to it as follows:
CASE
When
EXISTS (
SELECT * FROM branch_working_hours
WHERE (branch_working_hours.branch_id = branches.id AND branch_working_hours.day LIKE UCASE(DAYNAME(CURRENT_DATE())))
AND ((CURRENT_TIME() BETWEEN branch_working_hours.from1 AND branch_working_hours.to1 )
OR (branch_working_hours.is_24_hours_open= 1)
OR (CURRENT_TIME() BETWEEN branch_working_hours.from2 AND branch_working_hours.to2)
OR (branch_working_hours.to1 < branch_working_hours.from1 AND CURRENT_TIME() > branch_working_hours.from1 )
OR (branch_working_hours.to2 < branch_working_hours.from2 AND CURRENT_TIME() > branch_working_hours.from2))
) THEN 'OPEN'
WHEN EXISTS (
SELECT * FROM branch_working_hours
WHERE (branch_working_hours.branch_id = branches.id AND branch_working_hours.day LIKE UCASE(DAYNAME(SUBDATE(CURRENT_DATE(), 1))))
AND (
(branch_working_hours.to1 < branch_working_hours.from1 AND CURRENT_TIME() BETWEEN '00:00:00' AND branch_working_hours.to1)
OR (branch_working_hours.to2 < branch_working_hours.from2 AND CURRENT_TIME() BETWEEN '00:00:00' AND branch_working_hours.to2)
)
) THEN 'OPEN'
ELSE 'CLOSED'
end AS intrvl
Upvotes: 1