账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    gorm sql查询问题
    • 2020-01-01 00:00
    • 10
    62
    0

    数据如下

    issue_type taskstatus
    TEST 1
    RDC 0
    RDC 0
    RDC 0
    RDC 0
    RDC 0
    RDC 0
    OPS 0
    RDC 0
    RDC 0
    select a.issue_type as issue_type , a.totalcount as totalcount , b.donecount as donecount  from (select issue_type, count(issue_type) as totalcount from coot_work_order  group by issue_type) as a, (select issue_type, count(taskstatus) as donecount from coot_work_order where taskstatus = 0  group by issue_type) as b where a.issue_type = b.issue_type;

    通过sql执行结果为

    [{OPS 1 0} {RDC 8 0} {TEST 1 1}]

    定义struct

    type TotalIssue struct {
        IssueType string `json:"issue_type"`
        Totalcount     int    `json:"totalcount"`
        Donecount int `json:"donecount"`
    }
    
    var result []TotalIssue
    sql := "select a.issue_type as issue_type , a.totalcount as totalcount , b.donecount as donecount  from (select issue_type, count(issue_type) as totalcount from coot_work_order  group by issue_type) as a, (select issue_type, count(taskstatus) as donecount from coot_work_order where taskstatus = 0  group by issue_type) as b where a.issue_type = b.issue_type;"
    db.Table("coot_work_order").Raw(sql).Scan(&result).Debug()

    通过go执行结果为

    [{OPS 0 0} {RDC 0 0} {TEST 0 0}]

    为啥呀,另外求教不写sql方式该怎么写

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 共 0 条
    • 老烟枪 普通会员 1楼

      在使用Go的ORM框架(如GORM)时,查询数据库的问题可能会出现在多个方面。以下是一些常见的问题和解决方案:

      1. orm select或orm selectList:当在ORM查询中使用select或selectList方法时,你需要确保你的数据库已经正确地设置了相应的列。例如,如果你的表有id,name和email三个列,你需要使用select或selectList方法选择id和email这两个列。

      go db.Where("id = ?", id).Select("name, email")

      1. orm insert或orm insertList:当你在ORM查询中使用insert或insertList方法时,你需要确保你的数据库已经正确地设置了相应的表和列。例如,如果你的表有id,name和email三个列,你需要使用insert或insertList方法插入id,name和email这三个列。

      go db.Insert(&User{ID: id, Name: name, Email: email})

      1. orm delete或orm deleteList:当你在ORM查询中使用delete或deleteList方法时,你需要确保你的数据库已经正确地设置了相应的表和列。例如,如果你的表有id,name和email三个列,你需要使用delete或deleteList方法删除id,name和email这三个列。

      go db.Delete(&User{ID: id})

      1. orm update或orm updateList:当你在ORM查询中使用update或updateList方法时,你需要确保你的数据库已经正确地设置了相应的表和列。例如,如果你的表有id,name和email三个列,你需要使用update或updateList方法更新id,name和email这三个列。

      go db.Update(&User{ID: id, Name: name, Email: email})

      1. orm bind或orm bindList:当你在ORM查询中使用bind或bindList方法时,你需要确保你的参数已经被正确地绑定到你的字段。例如,如果你有一个名为user的字段,你可以使用bind或bindList方法来绑定一个User对象。

      go db.Bound(&User{ID: id, Name: name, Email: email})

      以上是一些基本的查询问题和解决方案。具体的查询语法可能会根据你的数据库和ORM框架有所不同。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部