gpt4 book ai didi

MySQL查询错误

转载 作者:行者123 更新时间:2023-11-29 14:44:04 27 4
gpt4 key购买 nike

我有一个sql查询的问题如下,我需要删除一些数据,但是引用了这些记录的父项和子项,例如:

mysql> select * from menu;
+--------+-------------------+-----------+-----------------------------------+
| menuId | menuNome | menuIdPai | menuLink |
+--------+-------------------+-----------+-----------------------------------+
| 1 | A Empresa | 0 | empresa.php |
| 2 | Sobre Nós | 1 | sobre.php |
| 3 | Objetivos | 1 | objetivos.php |
| 4 | Contato | 0 | contato.php |
| 5 | Produtos | 0 | produtos.php |
| 6 | Informática | 5 | categoria.php?cat=informatica |
| 7 | Missão da Empresa | 2 | missao.php |
| 8 | Visão da Empresa | 2 | visao.php |
| 9 | Televisão | 5 | categoria.php?cat=televisao |
| 10 | Computadores | 6 | subcategoria.php?sub=computadores |
| 11 | Monitores | 6 | subcategoria.php?sub=monitores |
+--------+-------------------+-----------+-----------------------------------+

我删除了id为5的查询菜单以及他们所有的 child 和亲戚如果我进行正常选择,并使用要删除的数据,我通常会得到结果,请参阅:

mysql> select * from menu where menuId = 5 or menuIdPai in( ( select menuId from menu where menuIdPai = 5 or menuId = 5 ) );
+--------+--------------+-----------+-----------------------------------+
| menuId | menuNome | menuIdPai | menuLink |
+--------+--------------+-----------+-----------------------------------+
| 5 | Produtos | 0 | produtos.php |
| 6 | Informática | 5 | categoria.php?cat=informatica |
| 9 | Televisão | 5 | categoria.php?cat=televisao |
| 10 | Computadores | 6 | subcategoria.php?sub=computadores |
| 11 | Monitores | 6 | subcategoria.php?sub=monitores |
+--------+--------------+-----------+-----------------------------------+
5 rows in set (0.00 sec)

但是当我删除时:

mysql> delete from menu where menuId = 5 or menuIdPai in( ( select menuId from menu where menuIdPai = 5 or menuId = 5 ) );
ERROR 1093 (HY000): You can't specify target table 'menu' for update in FROM clause

如何从表中删除数据,它们在选择中返回?

最佳答案

不幸的是,MySQL 不支持在同一表的 UPDATE 或 DELETE 语句中进行 SELECT。 http://dev.mysql.com/doc/refman/5.6/en/delete.html “目前,您无法在子查询中从表中删除并从同一个表中进行选择。”

因此,您必须使用联接来执行此操作。

关于MySQL查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7352017/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com