doushichi3678 2013-01-15 15:37
浏览 104
已采纳

ON DUPLICATE KEY插入新行而不是更新

I have a little problem.

This is the SQL code I get:

INSERT INTO matches (match_id, league_name, league_id, test_one) 
VALUES (866860, 'Portugal',1,'testing') 
ON DUPLICATE KEY 
UPDATE league_name = 
  CASE match_id 
    WHEN 866860 
      THEN 'Portugal' END, 
league_id = 
  CASE match_id 
    WHEN 866860 
      THEN 1 END, 
test_one = 
  CASE match_id 
    WHEN 866860 
      THEN 'testing' END

The problem is, that it always insert a new row instead of updating the existing one. Is it because I need to make the "match_id" as AUTO_INCREMENT or anything else (at the moment, my "id" field is AUTO_INCREMENT)

  • 写回答

2条回答 默认 最新

  • dousunle5801 2013-01-15 15:44
    关注

    AUTO_INCREMENT is irrelevant here. But ON DUPLICATE KEY requires a key. More specifically, a unique key, such as primary key or a unique index.

    Given the column names, I suspect that match_id fails to be a primary key.

    Update: You also write a complicate set of CASE ... END constructs. Have a look at the VALUES() function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料