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. }
  6. ss := new(SumStruct)
  7. var err error
  8. var total float64
  9. total, err = engine.Where("id >?", 1).Sum(ss, "money")
  10. fmt.Printf("money is %d", int(total))
  • SumInt
  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
  1. ss := new(SumStruct)
  2. var err error
  3. var totals []float64
  4. totals, err = engine.Where("id >?", 1).Sums(ss, "money", "rate")
  5. 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")
  5. fmt.Printf("money is %d", total[0])