i have a table named itemorder with: userID
, itemID
, date
, status
, notes
in my MYSQL DB
.
The PK of the table is userID, itemID
I need to write an SQL query that will delete all rows who are 2 days old and status = 2. (this sql query will run in my server once a day).
I've written the following sql query:
SELECT *
FROM itemorder
WHERE
statusOrder=2
AND statusDate< (SELECT DATE_ADD(CURDATE(), INTERVAL -2 DAY))
the query returns all rows who are match to condition. however if i change the SELECT *
to DELETE
it doesn't work.
here is the code
DELETE
FROM itemorder
WHERE
statusOrder=2
AND statusDate<(SELECT DATE_ADD(CURDATE(), INTERVAL -2 DAY))
it says: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor -> Query Editor and reconnect.
from the error i understood that i can not delete as the query WHERE
doesn't identify each rows by it's PK. what can i do?
I read the topic Delete duplicate records from a SQL table without a primary key however still couldn't figure how to change my SELECT
to DELETE
.