MySQL 选择日期之间的mysql查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1469689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:09:29  来源:igfitidea点击:

Select mysql query between date?

mysqlselectdate

提问by Noah Heldman

How to select data from mysql table past date to current date? For example, Select data from 1 january 2009 until current date ??

如何从过去日期到当前日期的mysql表中选择数据?例如,选择从 2009 年 1 月 1 日到当前日期的数据??

My column "datetime" is in datetime date type. Please help, thanks

我的“日期时间”列是日期时间日期类型。请帮忙,谢谢

Edit:

编辑:

If let say i want to get day per day data from 1 january 2009, how to write the query? Use count and between function?

如果说我想从 2009 年 1 月 1 日起每天获取数据,那么如何编写查询?使用 count 和 between 函数?

回答by Noah Heldman

select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()

or using >=and <=:

或使用>=<=

select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()

回答by shakirthow

All the above works, and here is another way if you just want to number of days/time back rather a entering date

以上所有工作,如果您只想返回天数/时间而不是输入日期,这是另一种方法

select * from *table_name* where *datetime_column* BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY)  AND NOW() 

回答by Kerri

You can use now()like:

你可以使用now()像:

Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();

回答by CONvid19

Late answer, but the accepted answer didn't work for me.
If you set both start and end dates manually (not using curdate()), make sure to specify the hours, minutes and seconds(2019-12-02 23:59:59) on the end date or you won't get any results from that day, i.e.:

迟到的答案,但接受的答案对我不起作用。
如果您手动设置开始日期和结束日期(不使用curdate()),请确保在结束日期指定小时、分钟和秒( 2019-12-02 23:59:59),否则您将不会从当天获得任何结果,即:

This WILL includerecords from 2019-12-02:

将包括来自2019-12-02以下方面的记录:

SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02 23:59:59'

This WON'T includerecords from 2019-12-02:

不会包括来自2019-12-02以下方面的记录:

SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02'