查询同时修改同一张表问题.
mysql> UPDATE EACONTACTGROUPS A
SET GROUPNAME=(SELECT CONCAT(B.GROUPNAME,'-',A.GROUPNAME) FROM EACONTACTGROUPS B WHERE B.CORPID=A.CORPID AND B.USERID=A.USERID AND B.GROUPID=A.PARENTGROUPID)
WHERE A.PARENTGROUPID IS NOT NULL;
ERROR 1093 (HY000): You can't specify target table 'A' for update in FROM clause
上面是目前MYSQL5.0仍然有的限制,文档中说:
-
In general, you cannot modify a table and select from the same table in a subquery. For example, this limitation applies to statements of the following forms:
DELETE FROM t WHERE ... (SELECT ... FROM t ...);UPDATE t ... WHERE col = (SELECT ... FROM t ...);{INSERT|REPLACE} INTO t (SELECT ... FROM t ...);
Exception: The preceding prohibition does not apply if you are using a subquery for the modified table in the
FROM
clause. Example:
UPDATE t ... WHERE col = (SELECT (SELECT ... FROM t...) AS _t ...);
Here the prohibition does not apply because a subquery in the
FROM
clause is materialized as a temporary table, so the relevant rows in
t
have already been selected by the time the update to
t
takes place. 依据文档,改成下面的样子就行了:
mysql> UPDATE EACONTACTGROUPS A
SET GROUPNAME=(SELECT CONCAT(B.GROUPNAME,'-',A.GROUPNAME) FROM ( SELECT GROUPNAME,CORPID,USERID,GROUPID,PARENTGROUPID FROM EACONTACTGROUPS) B
WHERE B.CORPID=A.CORPID AND B.USERID=A.USERID AND B.GROUPID=A.PARENTGROUPID)
WHERE A.PARENTGROUPID IS NOT NULL;
Query OK, 16 rows affected (0.01 sec)
Rows matched: 16 Changed: 16 Warnings: 0
注:今天写一个删除语句时找到的一点资料.解决了一个简单的删除语句.呵呵,以后注意了,
原句:delete from menu_item where parent_id =(select menu_item_id from menu_item where menu_data like '%manageVendors%');
修改后:delete from menu_item where parent_id =(select temp.menu_item_id from (select m.menu_item_id,m.menu_data from menu_item m) temp where temp.menu_data like '%manageVendors%');
update lock_table set locking=1,exec_time=now(),lock_num=lock_num+1
where com_uid=#com_uid# and shop_uid=#shop_uid# and lock_type=#lock_type#
and not exists(
select count(*) from lock_table where com_uid=#com_uid# and locking= 1 and lock_type in
<iterate open="(" close=")" conjunction="," property="types">
#types[]#
</iterate>
)
update lock_table s,(
select count(*) num from lock_table where com_uid=#com_uid# and locking= 1 and lock_type in
<iterate open="(" close=")" conjunction="," property="types">
#types[]#
</iterate>
) n
set s.locking=1,s.exec_time=now(),s.lock_num=s.lock_num+1
where s.com_uid=#com_uid# and s.shop_uid=#shop_uid# and s.lock_type=#lock_type#
and n.num=0
相关推荐
### MySQL中“You can’t specify target table for update in FROM clause”错误解决方法 在MySQL数据库管理过程中,遇到SQL语法错误是家常便饭,其中一种较为常见的错误是“You can’t specify target table for ...
在MySQL数据库中,"You can’t specify target table for update in FROM clause"是一个常见的错误提示,意味着在同一个SQL语句中,你不能直接在一个`UPDATE`或`DELETE`语句的`FROM`子句中引用你想要更新或删除的表...
在MySQL中,错误1093 - "You can't specify target table for update in FROM clause" 是一个常见的问题,它发生在尝试在`UPDATE`语句的`FROM`子句中直接引用要更新的同一张表时。这个错误表示MySQL不支持在`UPDATE`...
在MySQL数据库中,错误代码1093 - "You can’t specify target table ‘t’ for update in FROM clause" 是一个常见的错误,它通常发生在尝试在一个`UPDATE`语句的`FROM`子句中直接引用要更新的目标表时。...
...
错误消息"You can't specify target table 'wms_cabinet_form' for update in FROM clause"清楚地指出,在同一个UPDATE语句中,你不能先从`wms_cabinet_form`表中选择一些值,然后立即更新这个表。 在原始的UPDATE...
在平常的项目中,经常会碰到这样的问题:我需要在一张标中同时更新和查询出来的...结果却报错,报错信息为:You can't specify target table 'tb_test' for update in FROM clause,不能在同一语句中update,select同
导致了`java.sql.SQLException: You can’t specify target table ‘chat_messages’ for update in FROM clause`的错误。为了解决这个问题,我们需要对查询部分进行修改,确保它不直接引用正在插入的表。 修正后的...
这会导致错误 `[Err] 1093 – You can't specify target table 'a' for update in FROM clause`。MySQL不允许在`SET`或`WHERE`子句中直接引用被更新的表进行子查询。这是因为在执行过程中,MySQL无法确定子查询的...
5. 更新同一表数据的问题:`ERROR 1093 (HY000): You can't specify target table 'context' for update in FROM clause`表示不能在FROM子句中直接更新同一张表。 6. 主键自动增长问题:`ERROR 1062`可能是由于试图...
错误提示是 `ERROR 1093 (HY000): You can't specify target table 'apples' for update in FROM clause.` 这意味着MySQL不允许在 `UPDATE` 语句的 `FROM` 子句中直接引用要更新的表。 解决办法: 尽管MySQL不支持...
picture postcard showing the area you live in and some beautiful stamps for my kids who are stamp collectors. Do not use an envelop, I collect USED postcards sent to me. Write on the postcard that it ...
这里要注意,子查询需要使用别名,并且需要使用`SELECT * FROM (…)`的格式,避免`You can't specify target table 'a_tmp' for update in FROM clause`错误。 **高级方法:多字段去重** 当需要根据多个字段进行...