Quantcast
Channel: Adam Zwakk
Viewing all articles
Browse latest Browse all 10

Query of the day: Meal Times

$
0
0

I have a table like this:

id name starttime endtime
1 Breakfast 01:00:00 11:00:00
2 Lunch 11:00:00 16:00:00
3 Dinner 16:00:00 23:00:00

This query will figure out which time range the current time fits in. In this case, it figures out if we’re in breakfast-zone or lunch-land.

SELECT 
	name,
	IF(CURTIME() BETWEEN starttime AND endtime,1,0) AS active
FROM 
	meal_times

If you’re not used to using BETWEEN, here’s a way using the > and < operators.

SELECT 
	name,
	IF(CURTIME() >= starttime AND CURTIME() <= endtime,1,0) AS active
FROM 
	meal_times

This makes a seperate field called active and sets it to 1 if the meal is within the current time, otherwise it’s 0.


Viewing all articles
Browse latest Browse all 10

Trending Articles