【问题标题】:Delete from table where key not found in other table?从其他表中找不到键的表中删除?
【发布时间】:2012-11-06 03:58:02
【问题描述】:

表一:

  • 身份证
  • 姓名

表2:

  • 身份证
  • other_table_id
  • table_1_id
  • ..

基本上我想做的是

Delete from table_1 
where id not in (select table_1_id 
                 from table_2 
                 group by table_1_id);

哪个应该起作用,我想知道子查询是否是执行此操作的最佳方法/还有其他方法吗?

【问题讨论】:

    标签: mysql sql subquery


    【解决方案1】:

    我更喜欢使用JOIN 而不是子查询

    DELETE a FROM table_a a
                LEFT JOIN table_2 b
                    ON a.ID = b.table_1_id
    WHERE   b.table_1_id IS NULL
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多