duanqi5114 2018-01-09 21:14
浏览 56

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

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.

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
  • 写回答

1条回答 默认 最新

  • doudan4834 2018-01-09 21:32
    关注

    You can find a list of the MySQL date and time functions here. The interesting one for this question is DATEDIFF() which returns the difference between two dates in days.

    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:

    SELECT * from my_table
    

    To only select rows with a date within a certain range, WHERE is used. 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.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题