【问题标题】:Delete data from table without creating temporary table从表中删除数据而不创建临时表
【发布时间】:2020-04-15 08:17:21
【问题描述】:

我有三个表,与下图匹配:

我需要从 join_table 中删除一些数据,其中标签列(table_right)和名称列(table_left)匹配某些条件。

我的解决方案是使用临时表:

create temporary table if not exists data_for_deletion
(select jt.id
 from join_table jt
          left join table_left tableLeft on jt.table_left_id = tableLeft.id
          left join table_right tableRight on jt.table_right_id = tableRight.id
 where tableLeft.name = 'name' and tableRight.label = 'label');

delete from join_table where id in (select id from data_for_deletion);

我的问题是:有没有其他方法可以在不创建临时表的情况下进行此类删除?

【问题讨论】:

标签: mysql database join select left-join


【解决方案1】:

你应该可以使用 MySQL 的多表 DELETE 语法:

DELETE jt
FROM join_table jt
JOIN table_left tl ON jt.table_left_id = tl.id
JOIN table_right tr ON jt.table_right_id = tr.id
WHERE tl.name = 'name' AND tr.label = 'label'

请注意,由于您有一个 WHERE 子句,它依赖于 table_lefttable_right 中的列,因此使用 LEFT JOIN 毫无意义,因为它无论如何都会转换为 INNER JOIN(请参阅manual)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多