简体   繁体   English

我想从Codeigniter(msql)的数据库表中选择最近三天的数据

[英]i want to pick last three days data from my database tables in codeigniter (msql)

i have there tables in my database and i want to pick last three days of data from my 3 different database tables and i also want to show if the data is same then it will show result otherwise show empty rows in codeigniter. 我的数据库中有表,我想从3个不同的数据库表中选择最后三天的数据,我还想显示数据是否相同,否则将显示结果,否则在codeigniter中显示空行。

my all there tables look like this ==> 我那里所有的表看起来像这样==>

id   temp   hum  rain  time
1     16    62    NR   2018-01-05 11:00:23
2     17    62    NR   2018-01-06 11:00:22
3     17    61    NR   2018-01-07 11:00:22
4     16    60    NR   2018-01-08 11:00:23

You can find a list of the MySQL date and time functions here . 您可以在此处找到MySQL日期和时间函数的列表。 The interesting one for this question is DATEDIFF() which returns the difference between two dates in days. 这个问题有趣的一个是DATEDIFF() ,它以天为单位返回两个日期之间的差。

It would have been helpful if you'd shown your SQL statement so far, but I'm going to guess that it's something like this: 如果您到目前为止已经显示了SQL语句,那将很有帮助,但是我想这是这样的:

SELECT * from my_table

To only select rows with a date within a certain range, WHERE is used. 若要仅选择日期在特定范围内的行,请使用WHERE In this case, we want only the rows where the time is less than three days from now: 在这种情况下,我们只需要时间少于现在的三天的行:

SELECT * from my_table WHERE DATEDIFF(NOW(), my_table.time) < 3

You might have to experiment a little to get exactly the result you want, depending on how you want to round off the days. 您可能需要进行一些尝试才能获得所需的准确结果,具体取决于您如何舍入时间。

If you want to get the results within three days to the second , you can use the same idea, but with timestamps: 如果你想获得的第二三天之内的结果,你可以用同样的想法,但时间戳:

SELECT * from my_table WHERE (CURRENT_TIMESTAMP() - 60*60*24*3) < UNIX_TIMESTAMP(my_table.time)

60*60*24*3 is the number of seconds in three days. 60 * 60 * 24 * 3是三天内的秒数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM