如何从MySQL数据库获取今天/昨天的数据?

时间:2021-04-27 16:59:40

I would like to retrive a TODAY'S data from the database, but I don't know how to do it. I would actually want to get the data from NOT the past 24 hours, I just want today's data (so based on the actual server time).

我想从数据库中检索一个今天的数据,但我不知道该怎么做。我实际上想从过去24小时获取数据,我只想要今天的数据(所以基于实际的服务器时间)。

I would also like to get data which was yesterday. Can anyone help me how to do it?

我还想获得昨天的数据。任何人都可以帮我怎么做?

Sample code:

示例代码:

"SELECT id FROM folk WHERE time = ???"

Thank you in advance!

先谢谢你!

5 个解决方案

#1


13  

I think you are looking for this:

我想你正在寻找这个:

"SELECT id FROM folk WHERE DATE(time) = CURDATE()"

time must be a field in you table that holds a reference to the row.

time必须是表中包含对行的引用的字段。

update

更新

To get yesterdays additions:

为了获得昨天的补充:

"SELECT id FROM folk WHERE DATE(time) = CURDATE() - 1"

update 2

更新2

To get all additions this month:

要在本月获得所有新增内容:

"SELECT id FROM folk 
WHERE MONTH(time) = MONTH(NOW()) AND YEAR(time) = YEAR(NOW())"

reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

参考:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

#2


3  

SELECT id FROM folk WHERE DATE(time) = DATE(NOW());

SELECT id FROM folk WHERE DATE(time) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY));

provided that 'time' has a proper date-time type

只要'时间'有适当的日期时间类型

#3


1  

Try something like this>>

试试这样的东西>>

SELECT id from folk  WHERE DAY( date ) = EXTRACT(DAY from (NOW() - inTERVAL 
1 DAY ) ) 

Refer this link http://www.webmasterworld.com/forum112/278.htm

请参阅此链接http://www.webmasterworld.com/forum112/278.htm

#4


1  

As you are using timestamp:

在使用时间戳时:

"SELECT id FROM folk WHERE time >= ".mktime(0, 0, 0)

That will select all data since beginning today.

这将从今天开始选择所有数据。

If you want to get all date not for today, you would do

如果你想得到今天的所有日期,你会这样做

"SELECT id FROM folk WHERE time < ".mktime(0, 0, 0)

To select data from yesterday, you would do:

要从昨天选择数据,您可以:

"SELECT id FROM folk WHERE time < ".mktime(0, 0, 0)." AND time >= ".mktime(0, 0, 0, date('m'), date('d')-1, date('Y'))

If you were to use DATETIME, just for reference, it would be something like:

如果您使用DATETIME,仅供参考,它将类似于:

"SELECT id FROM folk WHERE time >= '".date('Y-m-d').' 00:00:00."'"

#5


0  

Get current date by using something like following query

通过使用以下查询来获取当前日期

SELECT * 
 FROM your_table_name 
WHERE DAY('.$colum_name.') = DAY("2018-05-02")';

#1


13  

I think you are looking for this:

我想你正在寻找这个:

"SELECT id FROM folk WHERE DATE(time) = CURDATE()"

time must be a field in you table that holds a reference to the row.

time必须是表中包含对行的引用的字段。

update

更新

To get yesterdays additions:

为了获得昨天的补充:

"SELECT id FROM folk WHERE DATE(time) = CURDATE() - 1"

update 2

更新2

To get all additions this month:

要在本月获得所有新增内容:

"SELECT id FROM folk 
WHERE MONTH(time) = MONTH(NOW()) AND YEAR(time) = YEAR(NOW())"

reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

参考:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

#2


3  

SELECT id FROM folk WHERE DATE(time) = DATE(NOW());

SELECT id FROM folk WHERE DATE(time) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY));

provided that 'time' has a proper date-time type

只要'时间'有适当的日期时间类型

#3


1  

Try something like this>>

试试这样的东西>>

SELECT id from folk  WHERE DAY( date ) = EXTRACT(DAY from (NOW() - inTERVAL 
1 DAY ) ) 

Refer this link http://www.webmasterworld.com/forum112/278.htm

请参阅此链接http://www.webmasterworld.com/forum112/278.htm

#4


1  

As you are using timestamp:

在使用时间戳时:

"SELECT id FROM folk WHERE time >= ".mktime(0, 0, 0)

That will select all data since beginning today.

这将从今天开始选择所有数据。

If you want to get all date not for today, you would do

如果你想得到今天的所有日期,你会这样做

"SELECT id FROM folk WHERE time < ".mktime(0, 0, 0)

To select data from yesterday, you would do:

要从昨天选择数据,您可以:

"SELECT id FROM folk WHERE time < ".mktime(0, 0, 0)." AND time >= ".mktime(0, 0, 0, date('m'), date('d')-1, date('Y'))

If you were to use DATETIME, just for reference, it would be something like:

如果您使用DATETIME,仅供参考,它将类似于:

"SELECT id FROM folk WHERE time >= '".date('Y-m-d').' 00:00:00."'"

#5


0  

Get current date by using something like following query

通过使用以下查询来获取当前日期

SELECT * 
 FROM your_table_name 
WHERE DAY('.$colum_name.') = DAY("2018-05-02")';