how to update swap values of two rows with single query

对着背影说爱祢 提交于 2019-11-27 01:23:04

问题


Is there a query with which i can exchange the values of two rows with single query?


回答1:


you can see the solution in this article

http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/

look at the : The elegant way , make a join to get the data from the 2 rows to be swapped in 1 row, after that make an update is easy.

example :

UPDATE
rules AS rule1
JOIN rules AS rule2 ON
( rule1.rule_id = 1 AND rule2.rule_id = 4 )
SET
rule1.priority = rule2.priority,
rule2.priority = rule1.priority
;



回答2:


UPDATE my_table SET a=@tmp:=a, a=b, b=@tmp;


来源:https://stackoverflow.com/questions/4517219/how-to-update-swap-values-of-two-rows-with-single-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!