【发布时间】: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);
我的问题是:有没有其他方法可以在不创建临时表的情况下进行此类删除?
【问题讨论】:
-
是的,在最后一个查询中使用您的选择作为子查询...... stackoverflow.com/questions/17548751/… 或使用内部 mysqltutorial.org/mysql-delete-join 删除
-
不,我不能。当我在最后一个查询中放入 select 语句时,出现以下错误: You can't specify target table 'join_table' for update in FROM 子句。
-
检查示例...或使用内部连接删除mysqltutorial.org/mysql-delete-join
标签: mysql database join select left-join