2021年1月7日星期四

How do I delete rows in one table where the ID matches another table row where a field is a certain value?

I admit the title question is convoluted; here is the situation.

I have two tables:

USERS Table  
id name status
1 Monica A
2 Ross A
3 Phoebe T
4 Chandler A
5 Rachel T
6 Joey A
PERMISSIONS Table  
user_id permission_id
1 32
1 51
4 12
6 2
3 5
5 22
2 18

What I want is a way to delete all rows from the PERMISSIONS table where that user's STATUS is "T" but how would I do that?

I had tried this:

DELETE FROM permissions  WHERE user_id IN (      SELECT user_id FROM users      WHERE status = 'T'  );  

However, SQL Server gives this error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

https://stackoverflow.com/questions/65622367/how-do-i-delete-rows-in-one-table-where-the-id-matches-another-table-row-where-a January 08, 2021 at 09:48AM

没有评论:

发表评论