4

I am using MySQL and would like to delete entries from table T1:

user_id  level_id  other_data
   1        5         ...
   2        7         ...
   :

where the user_id and level_id values appear together in table T2:

user_id  level_id
   1         5
   2         6

In this example, the first row would be deleted from table T1.

I tried:

delete from T1 where (user_id,level_id) in select user_id,level_id from T2;

but that has a syntax error.

1 Answer 1

8

You are pretty close. Try using exists:

delete from T1
    where exists (select 1
                  from t2
                  where t1.user_id = t2.user_id and t1.level_id = t2.level_id
                 );
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, both for the solution and the encouraging words. I hadn't known that "select 1" pattern.
@espertus . . . Some databases (such as Postgres) actually accept a syntax very close to what you wrote. All subqueries should be in parentheses, but otherwise Postgres would accept it.

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.