简体   繁体   中英

Delete from one table based off two other tables column?

I have 3 tables: t1,t2,t3

t1 has one column: AccountID

t2 columns: AccountID, Remaining, StatusID, DueDate

t3 columns: AccountID, Remaining, StatusID, DueDate

Here is my issue:

I need to delete all the rows in t1 table only if t2.Remaining = 0 AND t3.Remaining = 0

Can anybody help a brother out? I would greatly appreciate it, and will follow the best answer with an upvote/retweet your status/like your facebook page/give you money. HAHA j/k bout the money though.

Delete all rows from t1 for which there is a row in t2 and t3 where remaining equals 0. If there is a foreign key constraint, it is assumed it is cascading, so that all corresponding rows in t2 and t3 will automatically be deleted as well.

DELETE FROM t1
WHERE EXISTS
    (SELECT * FROM t2 WHERE t1.AccountId = AccountID AND Remaining = 0)
AND EXISTS
    (SELECT * FROM t3 WHERE t1.AccountId = AccountID AND Remaining = 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM