Sum

Sum methods

You can use Sum, SumInt, Sums and SumsInt methods to summerize columns, and you can also use sql conditions and struct conditions.

  • Sum
1. type SumStruct struct {
2. Id int64
3. Money int
4. Rate float32
5. }

7. ss := new(SumStruct)
8. var err error
9. var total float64
10. total, err = engine.Where("id >?", 1).Sum(ss, "money")
11. fmt.Printf("money is %d", int(total))
  • SumInt
1. type SumStruct struct {
2. Id int64
3. Money int
4. Rate float32
5. }

7. ss := new(SumStruct)
8. total, err:= engine.Where("id >?", 1).SumInt(ss, "money")
9. fmt.Printf("money is %d", total)
  • Sums
1. ss := new(SumStruct)
2. var err error
3. var totals []float64
4. totals, err = engine.Where("id >?", 1).Sums(ss, "money", "rate")

6. fmt.Printf("money is %d, rate is %.2f", int(total[0]), total[1])
  • SumsInt
1. ss := new(SumStruct)
2. var err error
3. var totals []int64
4. totals, err = engine.Where("id >?", 1).SumsInt(ss, "money")

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