如何使用单个查询更新两行的交换值 [英] how to update swap values of two rows with single query
本文介绍了如何使用单个查询更新两行的交换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个查询,我可以通过一次查询交换两行的值?
Is there a query with which i can exchange the values of two rows with single query?
推荐答案
您可以在本文中看到解决方案
you can see the solution in this article
http://www.microshell.com /database/sql/swap-values-in-2-rows-sql/
查看:一种优雅的方式,进行联接以从2行中获取数据,然后将其交换为1行,然后进行更新就很容易了.
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.
示例:
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
;
这篇关于如何使用单个查询更新两行的交换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文