Mysql: Swap data for different rows

前端 未结 4 1307
感情败类
感情败类 2020-12-14 03:11

Suppose a table fruits that looks like this:

------------------------------------------
| id |    name    |   color   | calories |
-------------         


        
4条回答
  •  情歌与酒
    2020-12-14 03:53

    Since ID is unique, it is difficult to just swap the IDs, it's easier to swap the column contents. A query like this might be what you need:

    UPDATE
      yourtable t1 INNER JOIN yourtable t2
      ON (t1.id, t2.id) IN ((1,5),(5,1))
    SET
      t1.color = t2.color,
      t1.name = t2.name,
      t1.calories = t2.calories
    

    Please see fiddle here.

提交回复
热议问题