Insert into a MySQL table or update if exists

前端 未结 11 2694
再見小時候
再見小時候 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条回答
  •  Happy的楠姐
    2020-11-21 05:22

    In my case i created below queries but in the first query if id 1 is already exists and age is already there, after that if you create first query without age than the value of age will be none

    REPLACE into table SET `id` = 1, `name` = 'A', `age` = 19
    

    for avoiding above issue create query like below

    INSERT INTO table SET `id` = '1', `name` = 'A', `age` = 19 ON DUPLICATE KEY UPDATE `id` = "1", `name` = "A",`age` = 19
    

    may it will help you ...

提交回复
热议问题