This repository has been archived on 2022-04-14. You can view files and clone it, but cannot push or open issues or pull requests.
manual-zh-CN/chapter-05/9.sums.md

1.0 KiB
Raw Permalink Blame History

Sum系列方法

求和数据可以使用Sum, SumInt, SumsSumsInt 四个方法Sums系列方法的参数为struct的指针并且成为查询条件。

  • Sum 求某个字段的和返回float64
type SumStruct struct {
    Id int64
    Money int
    Rate float32
}

ss := new(SumStruct)
total, err := engine.Where("id >?", 1).Sum(ss, "money")
fmt.Printf("money is %d", int(total))
  • SumInt 求某个字段的和返回int64
type SumStruct struct {
    Id int64
    Money int
    Rate float32
}

ss := new(SumStruct)
total, err := engine.Where("id >?", 1).SumInt(ss, "money")
fmt.Printf("money is %d", total)
  • Sums 求某几个字段的和, 返回float64的Slice
ss := new(SumStruct)
totals, err := engine.Where("id >?", 1).Sums(ss, "money", "rate")

fmt.Printf("money is %d, rate is %.2f", int(total[0]), total[1])
  • SumsInt 求某几个字段的和, 返回int64的Slice
ss := new(SumStruct)
totals, err := engine.Where("id >?", 1).SumsInt(ss, "money")

fmt.Printf("money is %d", total[0])