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';
2
  • It can be done in 1 query. Check this possible duplicate question Delete from two tables in one query Commented Aug 5, 2021 at 1: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. Commented Aug 5, 2021 at 7:19

1 Answer 1

1
START TRANSACTION;
DELETE FROM houses WHERE username='user1';
DELETE FROM houseusers WHERE username='user1';
COMMIT;
Sign up to request clarification or add additional context in comments.

6 Comments

Why would you need it to be in one query? Explain why this is a requirement.
It is a query to be run on a jdbc prepared statement and the number of affected rows needs to be known.
So run two statements. Retrieve the return value, the rows affected by each statement. Add those two numbers together.
Ok..I'll try it in the morning and tell you. Where I am it is very late.
To be clear, I mean run two prepared statements. You can't run two SQL statements in a single call to a prepared statement. Okay, good luck tomorrow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.