How to swap values of two rows in MySQL without violating unique constraint?

前端 未结 6 788
-上瘾入骨i
-上瘾入骨i 2020-12-01 07:29

I have a \"tasks\" table with a priority column, which has a unique constraint.

I\'m trying to swap the priority value of two rows, but I keep violating the constrai

6条回答
  •  一向
    一向 (楼主)
    2020-12-01 07:58

    Not sure if this would violate the constraints, but I have been trying to do something similar and eventually came up with this query by combining a few of the answers I found:

    UPDATE tasks as T1,tasks as T2 SET T1.priority=T2.priority,T2.priority=T1.priority WHERE (T1.task_id,T2.task_id)=($T1_id, $T2_id)
    

    The column I was swapping did not use a unique, so I am unsure if this will help...

提交回复
热议问题