在更新期间交换mysql中同一列内的值

时间:2021-01-21 23:10:26

My table looks like this,

我的桌子看起来像这样,

    RangeId    CellId  Some Coulmns more 
       101        1
       101        2

I ll get a list with [101,2],[101,1] now i have to swap cellId values in the above table. How to write an update query for this. I went through Swapping column values in MySQL but this swaps between two coulmns. Any suggestion..

我将获得[101,2],[101,1]的列表,现在我必须在上表中交换cellId值。如何为此编写更新查询。我经历了在MySQL中交换列值,但这在两个coulmns之间交换。任何建议..

EDIT: I am swapping the cells in my app and i ll get two cell ids. I have two just swap 2 with 1 and 1 with 2 and rest of the values in the rows remains the same

编辑:我在我的应用程序中交换单元格,我会得到两个单元格id。我有两个只是交换2与1和1与2和行中的其余值保持不变

EDIT2: The table doesnt have any Id column nor a primary key.

EDIT2:该表没有任何Id列或主键。

1 个解决方案

#1


0  

With your table as currently stated you cannot really do as you wish as there is no unique way to identify rows. I advise that you step back and look at what you are trying to do as it feels like either a: it's not been thought through, or b: you've not given enough information for this to really be solved

使用当前所述的表格,您无法按照自己的意愿进行操作,因为没有唯一的方法可以识别行。我建议你退后一步,看看你想要做什么,因为它感觉要么:它没有经过考虑,或者b:你没有给出足够的信息来真正解决

If b:, please provide more information on this table and the tables it links to and precisely what you are trying to achieve (yes I know you want to swap 2 numeric values however without knowing more information about the tables / what can be used to select it is VERY hard to advise accurately)

如果b:,请提供有关此表及其链接的表格以及您要实现的目标的更多信息(是的,我知道您要交换2个数值,但不知道有关表格的更多信息/可用于选择它是非常难以准确的建议)

note below was written for OPs original edit

下面的注释是为OPs原始编辑而写的

This isn't a nice way to do it but it may get what you are after, it relies on ID being a PKID

这不是一个很好的方法,但它可能会得到你所追求的,它依赖于ID是一个PKID

http://sqlfiddle.com/#!2/0c48c/2

#1


0  

With your table as currently stated you cannot really do as you wish as there is no unique way to identify rows. I advise that you step back and look at what you are trying to do as it feels like either a: it's not been thought through, or b: you've not given enough information for this to really be solved

使用当前所述的表格,您无法按照自己的意愿进行操作,因为没有唯一的方法可以识别行。我建议你退后一步,看看你想要做什么,因为它感觉要么:它没有经过考虑,或者b:你没有给出足够的信息来真正解决

If b:, please provide more information on this table and the tables it links to and precisely what you are trying to achieve (yes I know you want to swap 2 numeric values however without knowing more information about the tables / what can be used to select it is VERY hard to advise accurately)

如果b:,请提供有关此表及其链接的表格以及您要实现的目标的更多信息(是的,我知道您要交换2个数值,但不知道有关表格的更多信息/可用于选择它是非常难以准确的建议)

note below was written for OPs original edit

下面的注释是为OPs原始编辑而写的

This isn't a nice way to do it but it may get what you are after, it relies on ID being a PKID

这不是一个很好的方法,但它可能会得到你所追求的,它依赖于ID是一个PKID

http://sqlfiddle.com/#!2/0c48c/2