我试过这段代码
UPDATE testing_table t1
INNER JOIN testing_table t2 ON (t1.id, t2.id) IN ((1, 2),(2, 1))
SET t1.emp_id = t2.emp_id
但是好像有这个错误
#1062 - Duplicate entry '3' for key 'emp_id'
让我知道如何在不违反 mysql 中的唯一约束的情况下交换 2 列值
最佳答案
在 MySql 中,这并不容易做到,因为它会在更新期间检查每个单独记录的唯一约束,而不是在结束时(仅)。
所以为了允许列的值被交换,你需要允许列获得一个不会与任何约束冲突的临时值。因此,如果您有一个具有外键约束的列,和 也不允许 null
值,则很难在所有可能的情况下找到这样的值.
对于以下解决方案,必须允许 null
值。如果它们当前不是,则首先发出此语句以允许 emp_id 列为 null
:
alter table testing_table modify column emp_id int null;
然后:
start transaction;
update testing_table
set emp_id = null
where id = 1 and if(@emp1 := emp_id, 1, 1)
or id = 2 and if(@emp2 := emp_id, 1, 1);
update testing_table
set emp_id = if(id = 1, @emp2, @emp1)
where id in (1, 2);
commit;
说明
这将首先将值设置为null
,同时将它们之前的值存储在@emp1
和@emp2
中,并更新相同的记录保留在这些变量中的值,但交换了。
第一个 update
语句中的 if
只是为了确保赋值返回一个 true
表达式并使 where
条件成功。另请注意,短路评估将确保这些赋值仅在 and
运算符的左侧评估为 true
时发生。
关于php - 在不违反唯一约束的情况下交换 MySQL 中两行的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37157608/