dongshi1148 2017-05-22 22:52
浏览 742
已采纳

如何获取MySQL和Go中最后插入的行的ID?

How can I use this trick: How to get ID of the last updated row in MySQL? in Go (golang)?

I am using the go-sql-driver. It should work with these two queries but how can I do it in Go?

INSERT INTO table (unique_id) VALUES ("test")
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);

SELECT LAST_INSERT_ID();
  • 写回答

2条回答 默认 最新

  • douxuan0698 2017-05-22 23:08
    关注

    Working solution. It is as simple as that. I hope someone else will find this useful:

    stmt, err := db.Prepare("INSERT table SET unique_id=? ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)")
    
    res, err := stmt.Exec(unique_id)
    
    lid, err := res.LastInsertId()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器