首页 / 知识
关于数据库:在MySQL中交换列值
2023-04-13 19:35:00

Swapping column values in MySQL我有一个带有坐标的MySQL表,列名称为X和Y。现在我想交换此表中的列值,以使X成为Y并且Y成为X。最明显的解决方案是重命名列,但是我 不想进行结构更改,因为我不一定有这样做的权限。 这可能以某种方式与UPDATE有关吗? 更新表SET X = Y,Y = X显然不会满足我的要求。 编辑:请注意,我上面提到的权限限制有效地防止了使用ALTER TABLE或其他更改表/数据库结构的命令。 不幸的是,重命名列或添加新列不是选项。 wmh I just had to deal with the same and I'll summarize my findings.
The 这是使用临时变量的方法。感谢http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/的注释中的" IS NOT NULL"调整。没有它,查询将无法正常工作。请参阅文章末尾的表架构。如果其中一个为NULL,则此方法不交换值。使用没有此限制的方法#3。
Dipin在http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/的注释中再次提供了此方法。我认为这是最优雅,最干净的解决方案。它适用于NULL和非NULL值。
我想出的另一种方法似乎可行:
本质上,第一个表是要更新的表,而第二个表是用来从中提取旧数据的。 这是我的测试架构:
您可以使用X和Y求和并减去相反的值
这是一个示例测试(它适用于负数)
这是正在进行的交换
试试看 !!! 以下代码适用于我的快速测试中的所有方案:
wmh UPDATE table SET X=Y, Y=X will do precisely what you want (edit: in PostgreSQL, not MySQL, see below). The values are taken from the old row and assigned to a new copy of the same row, then the old row is replaced. You do not have to resort to using a temporary table, a temporary column, or other swap tricks. @D4V360: I see. That is shocking and unexpected. I use PostgreSQL and my answer works correctly there (I tried it). See the PostgreSQL UPDATE docs (under Parameters, expression), where it mentions that expressions on the right hand side of SET clauses explicitly use the old values of columns. I see that the corresponding MySQL UPDATE docs contain the statement"Single-table UPDATE assignments are generally evaluated from left to right" which implies the behaviour you describe. Good to know. wmh wmh Ok, so just for fun, you could do this! (assuming you're swapping string values) A nice bit of fun abusing the left-to-right evaluation process in MySQL. Alternatively, just use XOR if they're numbers. You mentioned coordinates, so do you have lovely integer values, or complex strings? Edit: The XOR stuff works like this by the way:
我相信以这种方式,最好将中间变量作为交换变量:
首先,它始终有效;第二,不管数据类型如何,它都能工作。 尽管两者
和
正常工作,顺便说一下,仅用于数字数据类型,这是防止溢出的责任,您不能在有符号和无符号之间使用XOR,也不能使用sum来产生溢出的可能性。 和
不管用 我们需要将其更改为
这是脚本:
Edit: About Greg's comment:
wmh
两种选择 wmh This surely works! I've just needed it to swap Euro and SKK price columns. :) The above will not work (ERROR 1064 (42000): You have an error in your SQL syntax) wmh wmh Assuming you have signed integers in your columns, you may need to use CAST(a ^ b AS SIGNED), since the result of the ^ operator is an unsigned 64-bit integer in MySQL. In case it helps anyone, here's the method I used to swap the same column between two given rows: where $1 and $2 are the keys of two rows and $3 is the result of the first query. wmh wmh I've not tried it but Might do it. Mark wmh 您可以更改列名,但这更像是一个技巧。但请注意这些列上可能包含的任何索引
表名称是客户。 更新客户设置a =(@ temp:= a),a = b,b = @temp 我检查了这工作正常。 在SQL Server中,可以使用以下查询:
如果要交换x到y和y到x的所有列;使用此查询。
我只需要将值从一列移到另一列(如存档)并重置原始列的值。
此示例将start_date和end_date交换为日期错误的记录(当执行ETL重大重写时,我发现一些开始日期晚于其结束日期。糟糕的程序员!)。 在原位,出于性能原因,我正在使用MEDIUMINT(例如朱利安天数,但0根为1900-01-01),所以我可以在WHERE条件下执行mdu.start_date> mdu.end_date。 PK分别位于所有3列上(出于操作/索引的原因)。
假设您想在tb_user中交换名字和姓氏的值。 最安全的是:
您可以在下面的查询中应用它,它对我来说非常理想。
使用单个查询交换列值 更新my_table SET a = @ tmp:= a,a = b,b = @ tmp; 干杯...! |
最新内容
相关内容
linux下用户权限命令?
linux下用户权限命令?,管理,系统,密码,地址,权限,时间,基础,信息,基本知识,工作,Linux用户以及权限相关常用命令总结sudo和su类似,有一些附加的linux改文件权限命令?
linux改文件权限命令?,系统,工具,档案,权限,文件,信息,命令,目录,选项,用户,在linux系统中修改文件权限使用的命令是在 Linux 中,要更改一个文linux复制命令重命名?
linux复制命令重命名?,系统,命令,文件,目录,百度,管理,人员,设计,名字,主体,Linux命令又多又杂,新手该如何分类?1、pwd命令使用pwd命令查找您linux查询表结构命令?
linux查询表结构命令?,系统,标准,信息,数据,地址,设备,时间,适当,软件,命令,linux下怎么用tree命令以树形结构显示文件目录结构1、以Ubuntu为linux加读权限命令?
linux加读权限命令?,系统,信息,档案,权限,工具,文件,数据,命令,目录,选项,linux可读可写权限对应的三种符号分别是r可读w可写入x可执行。第一linux添加命令权限?
linux添加命令权限?,密码,系统,权限,数字,地址,文件,命令,用户,表示,文件夹,在Linux中可以使用命令()针对文件newfiles.txt为所有用户添加执行linux权限转移命令?
linux权限转移命令?,数字,系统,命令,地址,密码,工作,权限,文件,第一,管理,linux目录命令,文件命令,权限设置命令1、CHMOD:文件/目录权限设置命linux查看文件权限命令?
linux查看文件权限命令?,时间,权限,文件,工作,系统,状态,命令,表示,用户,其他用户,linux怎么查看文件权限在 Linux 中,可以使用 ls -l 命令来查linux目录权限命令行?
linux目录权限命令行?,档案,网站,权限,系统,软件,信息,电脑,目录,命令,文件,linux权限不够怎么办?打开linux系统,在linux的桌面的空白处右击。linux查找权限命令?
linux查找权限命令?,系统,地址,工作,命令,文件,信息,概念,时间,标准,权限,Linux用户以及权限相关常用命令总结sudo和su类似,有一些附加的属性,管linux脚本赋权限命令?
linux脚本赋权限命令?,系统,档案,工作,命令,工具,脚本,权限,文件,程序,文件名,在Linux中执行文件时候权限不够,怎么解决1、修改权限可以解决该linux中命令重命名?
linux中命令重命名?,软件,名字,文件,命令,系统,工具,文件名,目录,指令,源文件,讲解Linux中的重命名命令rename的使用方法打开xshell软件连接上