简体   繁体   中英

If UNIX_TIMESTAMP is smaller in an SQL Query?

I'm trying to only get data from an db if unix timestamp is smaller than the recorded datetime in db and am having some trouble.

Here is what I got so far;

date_default_timezone_set("UTC");
$now = date("Y-m-d H:i:s", time());
$timestamp = strtotime($now); 
$timestamp2 = strtotime('-3 days', $timestamp);

$sql = "SELECT g.game_id, UNIX_TIMESTAMP(g.lastdraw)+259200 AS dbtimestamp FROM ".$prefix."_games g 
            INNER JOIN ".$prefix."_gameplayer gp
                ON g.game_id = gp.fk_game_id
            WHERE dbtimestamp < $timestamp2 AND gp.fk_player_id=$currplayer AND g.finished=0";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
        //if($row['dbtimestamp'] < $timestamp2){
        $thisgameid = $row['game_id'];

        if($thisgameid == 257748){
            echo '<b>GameID: '.$thisgameid.'</b><br>';
        } else {
            echo 'GameID: '.$thisgameid.'<br>';
        }
        echo 'DBtimestamp: '.$row['dbtimestamp'].'<br><br>';
    //}
}

I don't get any records out this way. But if I removes the "dbtimestamp < $timestamp2" in the sql query and removes the outcomment for "if($row['dbtimestamp'] < $timestamp2){" everything just works fine?

Any ideas where I go wrong?

Thanks in advance :-)

Why are you messing with PHP timestamps and MySQL date->timestamp values, when you could directly do this purely in MySQL using native date/time operations?

SELECT ..
WHERE g.lastdraw < (NOW() - INTERVAL 3 DAY)

This may not answer the question directly. However, there are some things worth pointing out that are too large for a comment.

The following:

$now = date("Y-m-d H:i:s", time());
$timestamp = strtotime($now); 
$timestamp2 = strtotime('-3 days', $timestamp);

Can be rewritten as:

$timestamp2 = strtotime('-3 days');

Furthermore, MySQL has several Date Time functions . Use something like DATE_ADD() instead of things like UNIX_TIMESTAMP(g.lastdraw)+259200 .

Not only is the query not returning any rows, the query should actually be throwing an error like this:

Error Code: 1054
Unknown column 'dbtimestamp' in 'where clause'

The problem with the SQL statement is that it's not valid to reference the alias for an expression in the SELECT list (eg dbtimestamp ) in the WHERE clause of the statement.

(And even if it were valid, you would not want to use that expression in the predicate. You want your predicate on the bare lastdraw column that you are using to derive that expression.)

To debug problems with SQL statements, the normative pattern is to echo out the contents of the string containing the SQL statement ( $sql in your case), and then test that statement by running it in another environment (eg mysql command line client).


One "fix" would be to replace the dbtimestamp alias in the WHERE clause with the actual expression., eg

WHERE UNIX_TIMESTAMP(g.lastdraw)+259200 < $timestamp2 

BUT... while that would get you around the 1054 error, that's not really the right fix. For performance, you don't really want to do the addition on on the left side. You'd be better off with a predicate of the form:

WHERE UNIX_TIMESTAMP(g.lastdraw) < $timestamp2 - 259200

NOTE that if the lastdraw column is not TIMESTAMP datatype, then you'd also want to avoid that call to the UNIX_TIMESTAMP function on the left side, and instead wrap the right side in a FROM_UNIXTIME function.

WHERE g.lastdraw < FROM_UNIXTIME( expr )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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