使用公共字段删除两个不同表的两个结果

时间:2013-10-16 20:04:48

标签: mysql sql join

在第一个表(first_table)之后:

id 
data
status

在第二个表(second_table)之后:

id
status
final_result

我想从final_result等于3的第二个表格中删除所有结果,并在第一个表格中status等于status之前的结果删除。根据我的询问:

DELETE FROM second_table WHERE final_result = '3'

我不知道怎么继续,你能帮助我吗?

2 个答案:

答案 0 :(得分:1)

你必须先删除第一张表的行。

DELETE FROM first_table where status in (select distinct status from second_table where final_result='3')
DELETE FROM second_table WHERE final_result = '3'

答案 1 :(得分:1)

DELETE FT, ST
FROM first_table AS FT
JOIN second_table AS ST
ON FT.status = ST.status 
AND ST.final_result = '3'