Correct INSERT .. ON DUPLICATE KEY syntax?

泪湿孤枕 提交于 2019-12-02 05:08:08

问题


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 using c#?

I tried this

MySqlCommand cmd2 = new MySqlCommand("INSERT INTO mapdisplay    
    (ID,Distance) 
  VALUES 
    (@r,@c,) 
  ON DUPLICATE KEY UPDATE mapdisplay   
    (Distance) 
  VALUES 
    (@c,)", conn);

but I think the syntax is wrong.


回答1:


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




回答2:


INSERT ... ON DUPLICATE KEY UPDATE ...



来源:https://stackoverflow.com/questions/7487179/correct-insert-on-duplicate-key-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!