Correct INSERT .. ON DUPLICATE KEY syntax?

后端 未结 2 484
故里飘歌
故里飘歌 2021-01-22 17:19

How can I check if a specific primary key (a string variable) already exists on the table and if not insert a new record otherwise just update the existing one with new values u

相关标签:
2条回答
  • 2021-01-22 17:57

    INSERT ... ON DUPLICATE KEY UPDATE ...

    0 讨论(0)
  • 2021-01-22 18:01
    INSERT INTO mapdisplay    
      (HexID,FlightNo,Lat,Lon,Alt,Course,Groundspeed,Verticalrate,Distance) 
    VALUES (@r,@c,@f,@t,@w,@q,@u,@e,@y)  
    ON DUPLICATE KEY UPDATE
      FlightNo = @c
      ,Lat = @f
      ,Lon = @t
      ,Alt = @w
      ,Course = @q
      ,Groundspeed = @u
      ,Verticalrate = @e
      ,Distance = @y;
    

    Look at the last example in this link:
    http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

    0 讨论(0)
提交回复
热议问题