使用JOIN和ORDER BY进行MySQL更新

时间:2013-09-03 10:45:01

标签: mysql sql

尝试运行以下查询:

UPDATE task_schedule
JOIN tasks ON (tasks.taskid=task_schedule.taskid)
SET task_schedule.user_position = @counter := @counter + 1
WHERE tasks.userid_owner = 6
ORDER BY task_schedule.product_position asc, task_schedule.productid asc

但是错误地使用了UPDATE和ORDER BY 错误。

无论如何我可以绕过这个?

1 个答案:

答案 0 :(得分:2)

ORDER BY上的多表语法不允许

UPDATE。以下是修复查询的一种方法:

UPDATE task_schedule
SET task_schedule.user_position = @counter := @counter + 1
WHERE exists (select 1
              from tasks t
              where t.userid_owner = 6 and
                    t.taskid = task_schedule.taskid
             )
ORDER BY task_schedule.product_position asc, task_schedule.productid asc