MySQL: could not select data with time range

本秂侑毒 提交于 2020-01-03 02:48:12

问题


I wanted to select data, if date is matched and time is in between start time and end time. I have tried in many ways, but i am not succeed.

check 3rd row in data table in below, start time 08:00:00 and end time is 11:00:00. it means 09:00:00 is between 08:00:00 and 11:00:00

Please help me.

Here is my Query:

SELECT * FROM (`rides`) WHERE `date` = '2013/04/30' AND `start_time` >= '9:00:00' AND `end_time` <= '9:00:00'

Table structure if you need to know the datatype of field:

CREATE TABLE IF NOT EXISTS `rides` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  ...,
  ...,
  `date` date NOT NULL,
  `start_time` time NOT NULL,
  `end_time` time NOT NULL
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;

Data in table:


回答1:


You wrote :

check 3rd row (...) it means 09:00:00 is between 08:00:00 and 11:00:00

I am assuming you expect this row in your result set. Obviously, your WHERE condition is wrong. What you want is:

WHERE ... start_time <= '9:00:00' AND '9:00:00' <= end_time

or alternatively

WHERE ... '09:00:00' BETWEEN start_time AND end_time

Notice the leading 0 is required in the above example. A more canonical form would be:

WHERE ... CAST('9:00:00' AS TIME) BETWEEN start_time AND end_time



回答2:


You can use between clause of MySQL Have a look here http://www.tutorialspoint.com/mysql/mysql-between-clause.htm




回答3:


From looking at your data, the query should not return any data because there are no suitable rows of data in your table.



来源:https://stackoverflow.com/questions/16298319/mysql-could-not-select-data-with-time-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!