Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
绕过MySQL中的子查询限制_Mysql - Fatal编程技术网

绕过MySQL中的子查询限制

绕过MySQL中的子查询限制,mysql,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

我使用此查询对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

此解决方案是什么?

设法将此解决方案组合在一起:

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=?;

设法整合此解决方案:

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=?;
试试这个:

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))
试试这个:

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))

看看这个:请提供一些示例,比如示例数据和预期输出。@genespos我已经看到了这个线程,我恐怕无法根据我的需要定制解决方案。我是一个彻头彻尾的sql noob。看看这个:请提供一些示例,例如示例数据和预期输出。@genespos我已经看到了这个线程,恐怕我无法根据我的需要定制解决方案。我是一个彻头彻尾的sql noob。谢谢,这个“选择”的概念看起来很奇怪。这个查询将如何执行?它能应付高流量吗?有没有优化的解决方案?@koder每个子查询都会变慢,但您需要尝试一下,因为如果查询在主键上工作,则速度会更快。如果这种方式太慢,你需要找到一种使用JOIN的方式,就像我给你的链接一样,这个“SELECT”的概念看起来很奇怪。这个查询将如何执行?它能应付高流量吗?有没有优化的解决方案?@koder每个子查询都会变慢,但您需要尝试一下,因为如果查询在主键上工作,则速度会更快。如果这种方式太慢,你需要找到一种方法,使用我给你的链接中的JOIN