V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
wo642436249
V2EX  ›  Go 编程语言

gorm 如何给某条数据的某字段值+1 或-1?

  •  
  •   wo642436249 · 2019-08-09 10:05:57 +08:00 · 4342 次点击
    这是一个创建于 1693 天前的主题,其中的信息可能已经有所发展或是发生改变。

    gorm 如何给某条数据的某字段值+1 或-1,不要先读取后更新的操作,太 Low 了感觉

    5 条回复    2019-08-09 18:03:42 +08:00
    janxin
        1
    janxin  
       2019-08-09 10:09:57 +08:00
    AppxLite
        2
    AppxLite  
       2019-08-09 10:10:08 +08:00
    昨晚刚刚遇到这个问题,官方文档案例有。
    {"price": gorm.Expr("price * ? + ?", 2, 100)}
    ("quantity", gorm.Expr("quantity - ?", 1)
    wo642436249
        3
    wo642436249  
    OP
       2019-08-09 10:14:53 +08:00
    @janxin 看了,看到二楼的回答后,秒懂,哈哈,第一次看没看懂
    Bruin
        4
    Bruin  
       2019-08-09 13:59:54 +08:00
    db.DbEngin.Exec("UPDATE xxxxx SET uv_count= uv_count + 1, update_time=? WHERE id = (?)", update_time, sumTmp.Id)
    wo642436249
        5
    wo642436249  
    OP
       2019-08-09 18:03:42 +08:00
    @Bruin 晓得了。身为新手,使用 github.com/go-redis/redis 包时遇到一个新的问题,当在 set 一个 key 时,如何设置过期时间?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1010 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:55 · PVG 03:55 · LAX 12:55 · JFK 15:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.