更新查询不起作用 - mysql

时间:2014-11-26 09:02:44

标签: mysql

以下是我的表结构:

+------+----------+
| product   | seq |
+------+----------+
|    a      | 1   |
|    b      | 1   |
|    a      | 2   |
|    a      | 3   |
|    b      | 2   |
+------+----------+

如果我的输入是a2那么,我需要更改序列号,输出将是

+------+----------+
| product   | seq |
+------+----------+
|    a      | 2   |
|    b      | 1   |
|    a      | 1   |
|    a      | 3   |
|    b      | 2   |
+------+----------+

我正在使用逻辑:

update mytable SET seq=if(seq=2,1,2), seq=if(seq=1,2,1) where product='a'

但它不会在表中进行任何更改

1 个答案:

答案 0 :(得分:0)

尝试这样

UPDATE table1 s1 , table1 s2 SET s1.seq=s2.seq, 
s2.seq=s1.seq WHERE s1.seq=2 and s2.seq=1 and
s1.product='a' and s2.product='a';