beego orm 查询count统计

怎么用beego orm查询下面的语句?

sql := "SELECT COUNT(1) AS countnumber, MONTH(date) as month FROM test GROUP BY MONTH(date)"

我用下面不行

type Count struct {
    month  int64
    countnumber int64
}

func Monthly()([]Count, error){
    o := orm.NewOrm()
    var countnum []Count
    sql := "SELECT COUNT(1) AS countnumber, MONTH(date) as month FROM test GROUP BY MONTH(date)"
    _, err := o.Raw(sql).QueryRows(&countnum)
    return countnum, err
}
已邀请:

heramerom

赞同来自:

没用过beego的orm, 不过那个type都是非导出变量吧。

type Count struct {
    Month  int64
    Countnumber int64
}

jinheking

赞同来自:

type Count struct { Month int orm:"column(month)" Countnumber int orm:"column(countnumber)" }

co:=models.Monthly() fmt.Println(co[0].Countnumber)

要回复问题请先登录注册