gorm sql查询问题

数据如下

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方式该怎么写

阅读 2.2k
1 个回答

type TotalIssue struct {
    IssueType  string `json:"issue_type"`
    TotalCount int    `gorm:"column:totalcount" json:"totalcount"`
    DoneCount  int    `gorm:"column:donecount" json:"donecount"`
}

结构体这样定义一下、

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题