【问题标题】:Delete rows in MySQL matching two columns in another table删除 MySQL 中与另一个表中的两列匹配的行
【发布时间】:2014-08-15 03:33:55
【问题描述】:

我正在使用 MySQL 并想从表 T1 中删除条目:

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

user_idlevel_id 值一起出现在表 T2 中的位置:

user_id  level_id
   1         5
   2         6

在本例中,第一行将从表 T1 中删除。

我试过了:

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

但这有语法错误。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    你很接近。尝试使用exists

    delete from T1
        where exists (select 1
                      from t2
                      where t1.user_id = t2.user_id and t1.level_id = t2.level_id
                     );
    

    【讨论】:

    • 感谢您的解决方案和鼓励的话。我不知道“选择 1”模式。
    • @espertus 。 . .一些数据库(例如 Postgres)实际上接受的语法与您编写的非常接近。所有子查询都应该放在括号中,否则 Postgres 会接受它。
    猜你喜欢
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    相关资源
    最近更新 更多