You can't specify target table 'A' for update in FROM clause

查询同时修改同一张表问题.

 

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值