简体   繁体   English

MySQL在X时间后选择带有时间戳的条目

[英]MySQL Select entries with timestamp after X time

I am attempting to populate a list of records within the last X seconds of the server time. 我正在尝试在服务器时间的最后X秒钟内填充记录列表。

My current query is.. 我当前的查询是..

mysql_query("SELECT player.name FROM player, spectate WHERE (player.pid = spectate.player) && spectate.bid=$bid");

This works currently to retrieve all entries. 当前可用于检索所有条目。 My first thought was to select the time field as well. 我的第一个想法就是选择时间字段。 And then use the mktime() function to find the time difference with the current server time, and only print records with a small difference. 然后使用mktime()函数查找与当前服务器时间的时差,并且仅打印时差很小的记录。

But I figured that it would be more effective to include WHERE ($x > $servertime - spectate.time + 1) along with my other statements. 但是我认为将WHERE($ x> $ servertime-spectate.time + 1)以及其他语句一起包含会更有效。

It doesn't work as I have it now what am I doing wrong? 它现在不起作用,因为我现在做错了什么?

Function with SELECT SELECT功能

function spectateJSON()
{
        $servertime = date("Y-m-d H:i:s");

        global $json, $bid;
        $specResult = mysql_query("SELECT player.name FROM player, spectate WHERE (player.pid = spectate.player) && spectate.bid=$bid && (20 > $servertime - spectate.time + 1)");
        $specResultCount=mysql_num_rows($specResult);
        $json.= '"spectate":['; 
        for($i=0; $i<$specResultCount; $i++)
        {
            $spectatorName=mysql_result($specResult,$i,"name");
            $json.='"'.$spectatorName.'"';
            if($i+1 != $specResultCount)$json.=",";
        }
        $json.=']';
}

function with UPDATE or INSERT 使用UPDATE或INSERT功能

if(isset($_SESSION['char']))
{
    $charId = $_SESSION['char'];
    $bid = $_GET['bid'];
    $servertime = date("Y-m-d H:i:s");

    $specResult = mysql_query("SELECT player FROM spectate WHERE player='$charId'");
    $specResultCount=mysql_num_rows($specResult);

    if($specResultCount > 0)
    {
        $Query=("UPDATE spectate SET bid='$bid' time='$servertime' WHERE player='$charId'");
        mysql_query($Query);
    }
    else
    {
        mysql_query("INSERT INTO spectate (bid, player, time) VALUES ('$bid','$charId','$servertime')");
    }
}

Thanks for your help, any other suggestions / critique is welcome as well =) 感谢您的帮助,也欢迎其他任何建议/批评=)


Update: 更新:

Spectate table entry example. 观察表条目示例。
bid: 169 出价:169
player: 1 球员:1
time: 2009-10-20 21:22:54 时间:2009-10-20 21:22:54

Player table entry example: 玩家表输入示例:
pid: 1 pid:1
uid: 1 uid:1
name: SpikeAi 名称:SpikeAi
score: 2000 得分:2000
wins: 0 获胜:0
lose: 0 输:0
tie: 0 并列:0

This should grab rows within the last minute: 这应该在最后一分钟内获取行:

SELECT

time

FROM

games

WHERE

`time` > DATE_SUB( NOW(), INTERVAL 1 MINUTE )

On a timestamp column type seemed to work for me. timestamp列上,类型似乎对我有用。 I don't think you necessarily need to generate the time in PHP as it should be the same time in MySQL assuming it's on the same server. 我认为您不一定需要在PHP中生成时间,因为假设它在同一台服务器上,那么它应该与MySQL中的时间相同。

Edit: This one is more compact... 编辑:这是一个更紧凑的...

SELECT * FROM games

WHERE

time > now() - INTERVAL 1 MINUTE

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

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