I have 3 tables MySQL (MyIsam):
user (id), message (id, userId, ...), archivedMessage (id, userId, ...)
How can I delete all the users having no message AND no archivedMessage?
To delete all rows older than 30 days, you need to use the DELETE with INTERVAL. Use < now() i.e. less than operator to get all the records before the current date.
We can use DELETE statement along with a WHERE clause, which identifies those multiple rows, to delete multiple rows from MySQL table.
The MySQL DELETE Statement The DELETE statement is used to delete existing records in a table.
You can specify multiple tables in a DELETE statement to delete rows from one or more tables depending on the condition in the WHERE clause. You cannot use ORDER BY or LIMIT in a multiple-table DELETE .
You could use not exists
:
delete from user
where not exists (select * from message m where m.userid = user.id)
and not exists (select * from archivedMessage am where am.userid = user.id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With