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方式该怎么写
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结构体这样定义一下、