简体   繁体   English

使用 where IN 子句从多个表中删除记录

[英]Delete records From Multiple Table using where IN clause

I want to delete data from 2 tables based on plan id which comes from the plan_temp table using WHERE IN .. I would be very grateful if you guys help me我想根据使用 WHERE IN 来自 plan_temp 表的计划 ID 从 2 个表中删除数据。如果你们帮助我,我将不胜感激

this is my code now I want to make this a single query这是我的代码,现在我想让它成为一个单一的查询

DELETE FROM plan WHERE id_plan IN(SELECT id_plan FROM plan_temp WHERE no=1184);


DELETE FROM plan_temp WHERE id_plan IN(SELECT id_plan FROM plan_temp WHERE no=1184);

Use an INNER JOIN to find the rows in both tables使用 INNER JOIN 查找两个表中的行

DELETE t1,t2 FROM plan t1
        INNER JOIN
    plan_temp t2 ON t2.id_plan = t1.id_plan 
WHERE
    t1.id_plan IN (SELECT id_plan FROM (SELECT * FROM plan_temp) t3 WHERE no=1184);

Please test all queries in a test environment with backup, to see if this is really what you want请在带有备份的测试环境中测试所有查询,看看这是否真的是您想要的

Unfortunately, you can't delete two separated tables.不幸的是,您不能删除两个单独的表。

DELETE FROM plan WHERE id_plan = 1184

DELETE FROM plan_temp WHERE id_plan = 1184

But, you can use Inner Join to delete both values from two tables.但是,您可以使用内部联接从两个表中删除两个值。 So, it has to be like that;所以,它必须是这样;

DELETE  plan, plan_temp FROM plan INNER JOIN plan_temp
Where
plan.id_plan = plan_temp.id_plan and id_plan = 1184;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Where子句从Mysql数据库中获取多个记录 - Fetch Multiple Records from Mysql Database Using Where Clause 使用WHERE子句从MySQL同一表的行中删除 - Delete using WHERE clause from the rows of the same table in MySQL MySQL:使用带有 WHERE 子句的 JOINS 从一个表中获取所有记录并从另一个表中获取可用记录 - MySQL: Using JOINS with WHERE clause to get all records from one table and available records from the other 从where子句中使用的表中删除 - Delete from table that is used in the where clause 如何使用FROM子句中的多个表以及WHERE子句中的某些搜索条件来计算一个表中的行? - How to count rows from one table using multiple tables in FROM clause and some search conditions in WHERE clause? 使用另一个子表中的where子句从表中删除行 - Delete rows from table with where clause from another table Laravel 5.4 不使用where 子句更新表中的所有记录 - Laravel 5.4 update all records in table without using where clause 如何在 where 子句和变量的帮助下从表中获取记录 - How to get records from a table with the help of a where clause and a variable 如何使用 where 子句显示表中的记录 - how can I display records from a table with a where clause 使用另一个表从表中删除记录? - delete records from table using another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM