I have a database where entries are inserted into tables with a timestamp (an int from PHP's time()
not an SQL timestamp). I want to make a query to drop all tables where the greatest timestamp is less than the expiry time (some time from the time the query is executed). Right now I have
$q = mysqli_query(..., "SHOW TABLES LIKE 'prefix%'");
if($q===FALSE) die(mysqli_error(...));
for($row in mysqli_fetch_array($q)){
$slice = array_slice($row, 0, 1);
$tbl = array_shift($slice);
mysqli_query(..., "DROP TABLE `$tbl` WHERE ((SELECT GREATEST (SELECT `time` FROM `$tbl`)) <= $expiry_time)");
}
This gives an SQL syntax error.
What is the correct way to achieve this? Also, is it possible to eliminate the PHP logic and just loop through each table and drop it if necessary all in SQL?