gorm sql查询问题

发布于 2022-09-11 22:21:23 字数 1638 浏览 19 评论 0

数据如下

issue_typetaskstatus
TEST1
RDC0
RDC0
RDC0
RDC0
RDC0
RDC0
OPS0
RDC0
RDC0
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方式该怎么写

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2022-09-18 22:21:23

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

结构体这样定义一下、

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文