简体   繁体   English

PHP MySQL-通过时差限制查询结果

[英]PHP MySQL - limit query result by time difference

This would be an example of database table: 这将是数据库表的示例:

在此处输入图片说明

PHP script should limit query result between NOW and time before 30 seconds PHP脚本应将查询结果限制在NOW和30秒之前的时间之间

To be clear: 要清楚:

In current time is 2012-03-23 03:28:00 and database is as descibed in example, resoult should be: "guest" 当前时间是2012-03-23 03:28:00并且数据库如示例所示,结果应为:“ guest”

This is a part of PHP script. 这是PHP脚本的一部分。

<?php
    $con = mysql_connect("localhost", "user", "pass");
    if (!$con) {
        die('Could not connect: '.mysql_error());
    }

    mysql_select_db("db", $con);

    $result = mysql_query('SELECT * FROM table LIMIT BY ...???...... ');
        while ($row = mysql_fetch_array($result)){
         echo $row['username'];
        }

    mysql_close($con);
?>

You don't want a LIMIT , you want a WHERE 您不想要LIMIT ,您想要WHERE

$result = mysql_query(
'SELECT * FROM table WHERE `timestamp` > NOW()- INTERVAL 30 SECOND '
);

In SQL, a LIMIT clause executes the whole query, and then only takes a certain "range" of consecutive rows. 在SQL中, LIMIT子句将执行整个查询,然后仅接受特定“范围”的连续行。 You can say "The first 50", or "From 20 to 30". 您可以说“前50个”或“从20到30”。

A WHERE clause limits the query on certain criteria, such as field contents. WHERE子句将查询限制为某些条件,例如字段内容。 That is what you want here. 那就是你想要的。

... WHERE (unix_timestamp(NOW()) - unix_timestamp(timestamp))<30 ..

要么

WHERE TIME_TO_SEC(TIMEDIFF(NOW(), timestamp))<30
    $result = mysql_query('SELECT * FROM table where date_sub(now(), timestamp ) < your_time_interval ');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM