MySQL:数据库是多少分钟前更新的?

发布于 2024-07-26 00:07:18 字数 140 浏览 3 评论 0原文

我需要在数据库中保留一个字段并以某种方式更新它,然后我需要检查该时间以查看它是否超过 30 分钟前,如果不是,距离 30 分钟还剩多少分钟?

我将使用 PHP+MySql 来完成此操作,有人可以告诉我最简单的方法吗?

谢谢!!

I need to keep a field in a data-base and update it with a time somehow, then later I need to check that time to see if it was over 30 minutes ago or not, and if not, how minutes left until 30?

I am going to be doing this with PHP+MySql can anyone tell me the simplest way to do this?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷到爆炸 2024-08-02 00:07:18

假设您想知道表中最后一次更新/插入发生多久了。
您可以使用 on update 子句设置一个带有时间戳字段的表

CREATE TABLE foo (
  id int auto_increment,
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  primary key(id),
  key(ts)
)

,然后查询 ts edit 中具有最大值的记录

SELECT
  TIMEDIFF(Now()-Interval 30 Minute, ts)
FROM 
  foo
ORDER BY
  ts DESC
LIMIT
  1

:如果您想获取例如过去 12 小时内插入/修改的所有记录,这也适用。

SELECT
  TIMEDIFF(Now()-Interval 30 Minute, ts)
FROM 
  foo
WHERE
  ts > Now()-Interval 12 hour
ORDER BY
  ts DESC

edit2:您也有可能对 感兴趣http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html

SHOW TABLE STATUS 返回以下字段:
...
更新时间
数据文件上次更新的时间。 对于某些存储引擎,该值为 NULL。 例如,InnoDB在其表空间中存储多个表,并且数据文件时间戳不适用。 对于MyISAM,使用数据文件时间戳; 但是,在 Windows 上,时间戳不会因更新而更新,因此该值不准确。

Let's assume you want to know how long ago the last update/insert in the table occurred.
You can set up a table with a timestamp field with an on update clause

CREATE TABLE foo (
  id int auto_increment,
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  primary key(id),
  key(ts)
)

and then query the record with the largest value in ts

SELECT
  TIMEDIFF(Now()-Interval 30 Minute, ts)
FROM 
  foo
ORDER BY
  ts DESC
LIMIT
  1

edit: This also works if you want to get all records that have been inserted/modified within e.g. the last 12 hours.

SELECT
  TIMEDIFF(Now()-Interval 30 Minute, ts)
FROM 
  foo
WHERE
  ts > Now()-Interval 12 hour
ORDER BY
  ts DESC

edit2: there's also an off chance you might be interested in http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html:

SHOW TABLE STATUS returns the following fields:
...
Update_time
When the data file was last updated. For some storage engines, this value is NULL. For example, InnoDB stores multiple tables in its tablespace and the data file timestamp does not apply. For MyISAM, the data file timestamp is used; however, on Windows the timestamp is not updated by updates so the value is inaccurate.

水波映月 2024-08-02 00:07:18

我可以将所有插入和更新 MySql 调用包装在一个函数中,如下所示:

function MySqlQuery($query, $res){
    $result = mysql_query($qs, $res);
    if($result === false){
        mysql_query("QUERY STRING TO UPDATE FIELD IN DATABASE WITH NEW TIME", $res);
    }
    return $result;
}

将“用新时间更新数据库中的查询字符串”替换为实际的更新查询,您应该可以开始了。

I could wrap all you insert and update MySql calls in a function something like the following:

function MySqlQuery($query, $res){
    $result = mysql_query($qs, $res);
    if($result === false){
        mysql_query("QUERY STRING TO UPDATE FIELD IN DATABASE WITH NEW TIME", $res);
    }
    return $result;
}

Replace the "QUERY STRING TO UPDATE FIELD IN DATABASE WITH NEW TIME" with an actual update query and you should be good to go.

枫以 2024-08-02 00:07:18

我所做的就是在最新记录上加上时间戳。 使用 MySQL 查询拉取最新记录,然后使用 mysql 获取数组函数获取最后一条记录的时间。 对于使用仅随时间更新的数据库也是如此。

您可以使用将当前时间与记录上的时间进行比较的函数来操纵该时间。 从那里您可以显示自上次发布以来的时间,如果超过 30 分钟,您可以让它回显一条消息。

$currenttime = /* PHP Time Formatting you wish to use. */

$query = mysql_query("SELECT time FROM database");
$row = mysql_fetch_array($query);

echo "Last Record Posted @" . $row['time'];

$timesince = $currenttime - $row['time'];
echo "Time Since Last Post:" . $time - $row['time'];

if($timesince >= "30"){
echo "Over 30 Minutes!";
}

如果您有任何疑问,请告诉我。 上面的代码应该让您了解它是如何工作的,但这只是一个粗略的示例。

祝你好运!

编辑:::

抱歉,我读错了问题,您仍然需要将时间输入数据库。 您仍然可以使用上面的代码来拉取时间,看看是否大于 30 分钟。

对于时间戳,请查看 PHP 时间手册。 您需要为 MySQL 输入和我上面发布的代码选择相同的时间格式。

What I do is, put a Time Stamp on the latest record. Pull the latest record with a MySQL Query and then use the mysql fetch array function to get the time of that last record. This goes the same for using a database that is updated with the time only.

You would be able to manipulate that time with a function that compares the current time to the time on the record. From there you can display the time since last posting, and if it is over 30 minutes you can make it echo a message.

$currenttime = /* PHP Time Formatting you wish to use. */

$query = mysql_query("SELECT time FROM database");
$row = mysql_fetch_array($query);

echo "Last Record Posted @" . $row['time'];

$timesince = $currenttime - $row['time'];
echo "Time Since Last Post:" . $time - $row['time'];

if($timesince >= "30"){
echo "Over 30 Minutes!";
}

Let me know if you have any questions. The above code should give you an idea of how it would work, but it is a rough example.

Best of Luck!!

EDIT:::

Sorry, I misread the question, You would still need to enter the time into the database. You can still use the above code to pull the time and see if it is greater than 30 minutes or not.

For the Time Stamp check out the PHP Time Manual. You will want to pick the same time format for both the MySQL Input and the code I posted above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文