I have 2 tables setup like this:
Items:
id (int)
name (varchar)
category (int)
last_update (timestamp)
Categories
id (int)
name (varchar)
tree_id (int)
I want to delete all the records from Items, who’s last_update is NOT today, and who’s corresponding categories.tree_id is equal to 1. However, I don’t want to delete anything from the Categories table, just the Items table. I tried this:
$query = "DELETE FROM items USING categories, items
WHERE items.category = categories.id
AND categories.tree_id = 1
AND items.last_update != '".date('Y-m-d')."'";
However this just seems to delete EVERY record who’s tree_id is 1. It should keep items with a tree_id of 1, as long as their last_update field is today
What am I missing?
You say that last_update contains time-stamps – I assume UNIX time-stamp. You can never find a record with the same time-stamp as when the stamp was executed and you can not compare a time-stamp with formatted date, they’ll never correspond. So you need to store the data in last_update column in a date(Y-m-d) format so that compare those which are not equal.