[英]update table based on subquery of table
I have am using a closure table for some page heirarchy. 我正在将闭包表用于某些页面层次结构。 I want to be able to delete a page and update the level of the children it leaves.
我希望能够删除页面并更新其离开的子级。
par child level
1 1 0
1 2 1
2 2 0
1 3 2
2 3 1
3 3 0
1 4 3
2 4 2
3 4 1
4 4 0
Prior to deleting page 3 I've attempted to update the levels and then deleteing reocrds for page 3 the goal of such being: 在删除第3页之前,我尝试过更新级别,然后删除第3页的录音,其目的是:
par child level
1 1 0
1 2 1
2 2 0
1 4 2
2 4 1
4 4 0
describing this with a (invalid) suquery like so: 用(无效)suquery描述此内容,如下所示:
UPDATE tbl_page_structures
SET page_level = page_level - 1
WHERE
child IN ( SELECT child FROM tbl_page_structures WHERE par = 3 )
AND page_level != 0;
DELETE ... where par=3 or child=3;
which obviously fails on the update. 这显然在更新上失败。
Ideally would like to complete in one query but if tmp able is way to go then so be it - performace on this is more important that sweet sql sweetness... 理想情况下,它想在一个查询中完成,但是如果能够使用tmp的话,那就这样吧-在这方面执行Performance较甜的sql甜蜜更为重要...
Try: 尝试:
UPDATE tbl_page_structures
SET page_level = page_level - 1
WHERE
child IN (SELECT * FROM( SELECT child FROM tbl_page_structures WHERE par = 3 ))
AND page_level != 0;
DELETE ... where par=3 or child=3;
As @Mark said: 正如@Mark所说:
Currently, you cannot update a table and select from the same table in a subquery.
当前,您无法更新表并在子查询中从同一表中选择。
Source: http://dev.mysql.com/doc/refman/5.5/en/update.html 资料来源: http : //dev.mysql.com/doc/refman/5.5/en/update.html
But you can if you do the inner select in a sub-sub-query. 但是您可以在子查询中进行内部选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.