Insert into a MySQL table or update if exists

前端 未结 11 2647
再見小時候
再見小時候 2020-11-21 04:27

I want to add a row to a database table, but if a row exists with the same unique key I want to update the row.

For example:



        
11条回答
  •  余生分开走
    2020-11-21 05:05

    INSERT IGNORE INTO table (id, name, age) VALUES (1, "A", 19);
    
    INSERT INTO TABLE (id, name, age) VALUES(1, "A", 19) ON DUPLICATE KEY UPDATE NAME = "A", AGE = 19;
    
    REPLACE INTO table (id, name, age) VALUES(1, "A", 19);
    

    All these solution will work regarding your question.

    If you want to know in details regarding these statement visit this link

提交回复
热议问题