Brian
Brian

Reputation: 1

MySQL query cerain hours

New to MySql and am trying to query a table to get just certain hours and minutes such as

05:00:00. Have been using following, but only getting the 05:00:00 hours but need both 05 and 11 hours...

SELECT *  FROM `UMHMwind` 
WHERE Hour(`dt`) = "05,11" AND MINUTE(`dt`) = "0". 

Probably a simple solution but have been unable to find anything helpful online.

Upvotes: 0

Views: 33

Answers (3)

Musa Haidari
Musa Haidari

Reputation: 2267

You want to check a value is equal to either a value OR another (I mean hours in this case): you can use:

  • = operator

... where HOUR(dt) = 5 or HOUR(dt) = 11 ...

  • IN operator

... where HOUR(dt) in (5, 11) ...

The second is a short-hand, so I suggest it.

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270653

The correct syntax is:

SELECT *
FROM UMHMwind
WHERE Hour(dt) IN (5, 11) AND MINUTE(dt) = 0

Note that the hour() and minute() function return integers, so you should compare the results to integers.

Upvotes: 1

Kermit
Kermit

Reputation: 34063

To specify a list of values, you use the IN clause:

SELECT *  FROM `UMHMwind` 
WHERE HOUR(`dt`) IN (5, 11) AND MINUTE(`dt`) = 0 

Note that when using functions around dates, indexes will not be used.

The documentation is your friend.

Upvotes: 2

Related Questions