在MySQL中解决子查询限制

时间:2015-11-13 07:33:41

标签: mysql

我有这个查询来重新排序MySQL表中的项目

Update Content.items SET list_order=list_order+1 Where list_order=
(Select list_order-1 From Content.items where id='41e31kufg');

但是我收到了这个错误:

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

在重新排序之前,有一个数据集示例

rowid    list_order
41e31kufg 1
62g88nfjs 2
99h84mlkd 3
92r63mkvf 4

重新排序后:

rowid    list_order
99h84mlkd 1
92r63mkvf 2
41e31kufg 3
62g88nfjs 4

这是什么解决方案?

2 个答案:

答案 0 :(得分:0)

管理以拼凑此解决方案:

Update Content.items SET list_order=list_order+1 Where list_order>= ? and 
list_order < (Select * From (Select list_order From Content.items where id=?) AS lists);
Update Content.items SET list_order=? WHERE id=?;

答案 1 :(得分:0)

试试这个:

UPDATE items 
SET list_order=list_order+1 
WHERE list_order= (
    SELECT (
        SELECT list_order-1 
        FROM (
            SELECT list_order 
            FROM items 
            WHERE id='41e31kufg') AS x))