正确的INSERT .. ON DUPLICATE KEY语法? [英] Correct INSERT .. ON DUPLICATE KEY syntax?

查看:95
本文介绍了正确的INSERT .. ON DUPLICATE KEY语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查表上是否已存在特定的主键(字符串变量),并且如果不插入新记录,则只需使用c#使用新值更新现有的主键?

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

我尝试过

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.

推荐答案

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;

请查看此链接中的最后一个示例:
http://dev.mysql.com/doc/refman /5.0/en/insert-on-duplicate.html

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

这篇关于正确的INSERT .. ON DUPLICATE KEY语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆