Sum系列方法

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

  • Sum 求某个字段的和,返回float64
  1. type SumStruct struct {
  2. Id int64
  3. Money int
  4. Rate float32
  5. }
  6. ss := new(SumStruct)
  7. total, err := engine.Where("id >?", 1).Sum(ss, "money")
  8. fmt.Printf("money is %d", int(total))
  • SumInt 求某个字段的和,返回int64
  1. type SumStruct struct {
  2. Id int64
  3. Money int
  4. Rate float32
  5. }
  6. ss := new(SumStruct)
  7. total, err := engine.Where("id >?", 1).SumInt(ss, "money")
  8. fmt.Printf("money is %d", total)
  • Sums 求某几个字段的和, 返回float64的Slice
  1. ss := new(SumStruct)
  2. totals, err := engine.Where("id >?", 1).Sums(ss, "money", "rate")
  3. fmt.Printf("money is %d, rate is %.2f", int(total[0]), total[1])
  • SumsInt 求某几个字段的和, 返回int64的Slice
  1. ss := new(SumStruct)
  2. totals, err := engine.Where("id >?", 1).SumsInt(ss, "money")
  3. fmt.Printf("money is %d", total[0])