MySQL on duplicate key update

前端 未结 2 804
时光取名叫无心
时光取名叫无心 2021-01-02 13:26

If I have query like this, how can I refer to values I have already given in update statement, so that I don\'t need to insert same data to query again? Example I would like

2条回答
  •  迷失自我
    2021-01-02 13:52

    This should work and is a little more elegant:

    INSERT INTO TABLENAME(col1, col2) 
    VALUES ('xxx', 'yyy') 
    ON DUPLICATE KEY UPDATE col1 = VALUES(col1)
    

    Note that you don't need to update the primary key part of the row. We know that's the same because there was a collision.

提交回复
热议问题