I have a table with over a 100 users that never finished the signin process. I'm trying to delete those users together with all data associated with them.
Since the table relations were not added by the previous developer working on this, I have to do all queries manually.
I came up with the following right now
The last of the 3 queries tries to delete all companies which don't have a company_id in the users table, so all companies that don't have a user associated with them.
The locations and tags constraint kick in, So I have to delete those first. That's how I built this query.. But it's a lot of code and it's hard to read. Any way to improve this?
delete from locations where company_id in (select id from companies
where not exists (select null from users where users.company_id =
companies.id));
delete from tags where company_id in (select id from companies where
not exists (select null from users where users.company_id =
companies.id));
delete from companies where not exists (select null from users where
users.company_id = companies.id);
What i've tried
- Using SET @todelete = (select...) : but it's throwing errors that my Subquery returns more than 1 record.
Am I doing this the right way? Or can it be simplified?
Thank you!