2

I have two tables: one contains user logins and the other contains user data. I would like to delete users who may or may not exist in the latter table but definitely exist in the former. How do I account for users who may or may not exist? Please note that it should be in one query....
I have tried:

DELETE houses,houseusers FROM houses INNER JOIN houseusers ON houseusers.username = houses.username WHERE houses.username='user1' OR houseusers.username='user1';
user14773854
  • 303
  • 4
  • 10
  • It can be done in 1 query. Check this possible duplicate question [Delete from two tables in one query](https://stackoverflow.com/questions/1233451/delete-from-two-tables-in-one-query/5126239) – Kuro Neko Aug 05 '21 at 01:51
  • @ErrBon I don't think this is a duplicate since I am asking about deleting from at least one table if a row does not exist in the second table. The question you have linked talks about deleting from exactly two tables. – user14773854 Aug 05 '21 at 07:19

1 Answers1

1
START TRANSACTION;
DELETE FROM houses WHERE username='user1';
DELETE FROM houseusers WHERE username='user1';
COMMIT;
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828