SELECT * FROM property
LEFT JOIN installmentagreement
ON property.PId=installmentagreement.PId
LEFT JOIN installments
ON installmentagreement.Id=installments.Id
LEFT JOIN rentagreement
ON property.PId=rentagreement.PId
WHERE property.PId=39
这是按预期工作的SELECT查询。我想删除此SELECT查询的结果。我使用了这个查询。
DELETE FROM property( SELECT * FROM property
LEFT JOIN installmentagreement
ON property.PId=installmentagreement.PId
LEFT JOIN installments
ON installmentagreement.Id=installments.Id
LEFT JOIN rentagreement
ON property.PId=rentagreement.PId
WHERE property.PId=39)
这也是。
DELETE FROM property
LEFT JOIN installmentagreement
ON property.PId=installmentagreement.PId
LEFT JOIN installments
ON installmentagreement.Id=installments.Id
LEFT JOIN rentagreement
ON property.PId=rentagreement.PId
WHERE property.PId=39
但它返回语法错误。我想将SELECT查询转换为DELETE查询。我正在使用的数据库是MySQL。如果有任何其他更好的解决方案,请建议。请帮忙!
答案 0 :(得分:1)
为了清晰起见,我建议使用别名:
SELECT *
FROM property as p
LEFT JOIN installmentagreement as ia
ON p.PId=ia.PId
LEFT JOIN installments as i
ON ia.Id=i.Id
LEFT JOIN rentagreement as ra
ON p.PId=ra.PId
WHERE p.PId=39
然后您可以轻松转换为删除语句,如下所示:
delete p
FROM property as p
LEFT JOIN installmentagreement as ia
ON p.PId=ia.PId
LEFT JOIN installments as i
ON ia.Id=i.Id
LEFT JOIN rentagreement as ra
ON p.PId=ra.PId
WHERE p.PId=39
如果您想在删除property
记录之前删除某些左连接表中的记录,您可以轻松更改delete语句中的别名:
delete ra
FROM property as p
LEFT JOIN installmentagreement as ia
ON p.PId=ia.PId
LEFT JOIN installments as i
ON ia.Id=i.Id
LEFT JOIN rentagreement as ra
ON p.PId=ra.PId
WHERE p.PId=39
答案 1 :(得分:0)
MySQL支持在一个语句中删除多个表,但您需要指定要从中删除的表:
DELETE p, ima, i, ra
FROM property p LEFT JOIN
installmentagreement ima
ON p.PId = ima.PId LEFT JOIN
installments i
ON ima.Id = i.Id LEFT JOIN
rentagreement ra
ON p.PId = ra.PId
WHERE p.PId = 39;
当然,您不必在DELETE
之后包含所有四个表格,只包括您要删除的表格。