【问题标题】:I want to fetch the latest id in flask mysql我想在烧瓶 mysql 中获取最新的 id
【发布时间】:2019-02-09 20:58:22
【问题描述】:

当我运行以下查询时,它给了我第一个 id,我想要最后一个 id,我想要那个 id 中的数据,我该怎么做?

cursor = mysql.connection.cursor()
sorgu = "Select * From site2"
result = cursor.execute(sorgu) 
if result > 0:
    articles = cursor.fetchone()        
    return render_template("site.html",articles = articles)
else:
    return render_template("site.html")

【问题讨论】:

    标签: python mysql-python


    【解决方案1】:

    首先从表中检索最大 ID(假设您的 ID 在添加行时向上递增。这意味着最大的是最后一个),然后在最终查询中使用它

    sorgu = "SELECT * FROM site2 WHERE ID = (SELECT MAX(ID) FROM site2)"
    

    如果您的数据库中有时间戳列,您也可以通过 MAX(timestap) 获取最新 ID

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-07
      • 2017-05-09
      • 1970-01-01
      • 2020-08-02
      • 2017-04-16
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      相关资源
      最近更新 更多