【问题标题】:How to get the exact date and time when mysql database table last updated?如何获取mysql数据库表上次更新的确切日期和时间?
【发布时间】:2016-08-18 08:48:32
【问题描述】:

大家好,我是 Web 开发新手,我在获取 mysql 数据库表上次更新的日期和时间时遇到问题,因为我必须在我的网页上显示它。我得到了正确的最后更新日期,但时间不正确,请帮助我。

 <?php

    $sql = "SHOW TABLE STATUS FROM MydatabaseName LIKE 'TableName'";
    $tableStatus = mysql_query($sql);

while ($array = mysql_fetch_array($tableStatus)) {
          $updatetime = $array['Update_time'];

          $datetime = new DateTime($updatetime);
          echo $updatetime ;
     }

 ?>

【问题讨论】:

  • 您的表格中是否有不同的日期和时间列?
  • @Priyanshu - 不,我没有将日期和时间存储在不同的列中。我正在为它寻找一些系统定义方法,它可以返回最后更新的日期和时间。

标签: php mysql


【解决方案1】:

如果你的MySQL版本比较新,可以查询information_schema.tables

 select substr(update_time,1,10) as date_updated,
        substr(update_time,12) as time_updated 
 from information_schema.tables
 where 
 table_schema = 'name_of_your_database' and 
 table_name = 'your_table_name';

请注意this may not work for user-defined tables if your MySQL engine is InnoDB。它可以像在 MyISAM 安装上宣传的那样工作。

【讨论】:

    【解决方案2】:

    如果这对你有帮助

    SELECT UPDATE_TIME
    FROM   information_schema.tables
    WHERE  TABLE_SCHEMA = 'dbname'
    AND TABLE_NAME = 'tabname'
    

    How can I tell when a MySQL table was last updated?

    【讨论】:

    • 嗨 Ashish - 在我的情况下,您能否将上述答案转换为 php 参考? Name' AND TABLE_NAME = '表名"; $tableStatus = mysql_query($sql);而 ( $row = mysql_fetch_assoc($tableStatus)) { $fetch[]= $row;回声$获取; } ?> 但得到空数组...
    【解决方案3】:

    因为你已经在 mysql 中标记了一个问题。

    你试过了吗? 看看这是否有帮助。

    select columns from table order by date_time column desc limit 1:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多