简体   繁体   中英

SQLite greater/less than operator giving false results

I have the following SQLite query. It is intended to delete all entries where the time has not been updated in the past three seconds.

$expireTime = time() - 3;
$dbh->query("DELETE FROM `whois_online` WHERE (strftime('%s', `time`) > $expireTime)");

This query does not return any entries in the table, even though it should. However, changing the > to < causes the entires to appear, even though the numbers it is comparing should not let this happen.

If I explicitly replace expireTime with a number:

sqlite> SELECT strftime('%s', `time`) from whois_online WHERE (strftime('%s', `time`) > 1400005440);

I get the following result:

1400005363
1400005365
1400005368
1400005443

This is clearly not logically correct, as all these numbers are less than 1400005440. What is going on here?

strftime() returns a string , not an integer. You need to use CAST() to convert the type.

SELECT CAST(strftime('%s', `time`) AS integer)
FROM whois_online
WHERE CAST(strftime('%s', `time`) AS integer) > 1400005440;

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