zoukankan      html  css  js  c++  java
  • Python--flask使用 SQLAlchemy查询数据库最近时间段或之前的数据

    Python--flask使用 SQLAlchemy查询数据库最近时间段或之前的数据

    博客说明

    文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!

    说明

    在操作数据库的时候,有一个需求是展示最近的记录,就需要使用查询最近几天的数据

    思路

    获取当前时间戳,根据时间来计算,然后查询范围

    from datetime import datetime, timedelta
    
    
    time_now = datetime.now()
    #最近30天数据
    model_user = User.query.filter(User.create_time >= time_now - timedelta(days=30)).all()
    #最近一周数据
    model_user = User.query.filter(User.create_time >= time_now - timedelta(days=7)).all()
    #最近1天数据
    model_user = User.query.filter(User.create_time >= time_now - timedelta(days=1)).all()
    #最近12小时
    model_user = User.query.filter(User.create_time >= time_now - timedelta(hours=12)).all()
    #最近半小时
    model_user = User.query.filter(User.create_time >= time_now - timedelta(seconds=30)).all()
    

    感谢

    万能的网络

    以及勤劳的自己
    关注公众号: 归子莫,获取更多的资料,还有更长的学习计划

  • 相关阅读:
    RabbitMQ 均衡调度(公平分发机制)
    RabbitMQ 循环调度
    模型绑定与验证笔记
    Controller总结
    JQuery事件绑定,bind与on区别
    View的呈现(一)ActionResult
    C#操作sql时注意点
    mvc4中的过滤器
    Bundle、Intent、SharedPreferences
    SharedPreferences的基本用法
  • 原文地址:https://www.cnblogs.com/guizimo/p/12990053.html
Copyright © 2011-2022 走看看