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

Error Code: 1093. You can't specify target table 'xxx' for update in FROM clause.

意思大概是:MySql不支持在Update语句中引用被update对象。

原SQL如下:

update sec_menu_item as m 

set 

    m.code = (

select 

            concat(CONCAT(pinyin(mp.title), '_'),

                        pinyin(m.title))

        from

            sec_menu_item as mp

        where

            mp.id = m.parent_id

);

报上述错误,改动后如下:

update sec_menu_item as m inner join (

select mp.id,mp.title from

            sec_menu_item as mp

) as tmp on m.parent_id = tmp.id

set m.code=concat(concat(pinyin(tmp.title),'-'),pinyin(m.title));

执行通过。

猜你喜欢

转载自jxlanxin.iteye.com/blog/2215650