Get data between two date on MySQL

喜夏-厌秋 提交于 2019-12-01 10:49:01

问题


How can I get the values between two dates.

I want to get the values between 2010-01-02 and 2010-01-04.

Example:

Value DateTime
A     2010-01-01 14:55:12
B     2010-01-02 14:55:12
C     2010-01-03 14:55:12
D     2010-01-04 14:55:12
E     2010-01-05 14:55:12

Thanks!


回答1:


Have a look at

expr BETWEEN min AND max

If expr is greater than or equal to min and expr is less than or equal to max, BETWEEN returns 1, otherwise it returns 0.

/

For best results when using BETWEEN with date or time values, you should use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.




回答2:


I know the answer has already been accepted, but I thought I'd add a bit more to what @astander has mentioned. I answered a post recently regarding date queries, and a useful way of extracting more useful information from the dates using a date dimension.

See the following StackOverflow question, and look to my answer...

Select all months within given date span, including the ones with 0 values



来源:https://stackoverflow.com/questions/2716336/get-data-between-two-date-on-mysql

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