Sql交换两条记录的某一个属性值

2023-10-14 15:08:27 浏览数 (1)

代码语言:sql复制
-- 属性交换
UPDATE sys_user su1
JOIN sys_user su2 ON ( su1.nick_name = '小明' AND su2.nick_name = '小红' ) 
OR ( su1.nick_name = '小红' AND su2.nick_name = '小明' ) 
SET su1.ding_user_id = su2.ding_user_id,
su2.ding_user_id = su1.ding_user_id;

-- 查询交换后的
SELECT
	* 
FROM
	sys_user su1
	JOIN sys_user su2 ON ( su1.nick_name = '小明' AND su2.nick_name = '小红' ) 
	OR ( su1.nick_name = '小红' AND su2.nick_name = '小明' );
sql

0 人点赞