116 lines
2.4 KiB
Go
116 lines
2.4 KiB
Go
|
|
package db
|
||
|
|
|
||
|
|
import (
|
||
|
|
. "code.hoteas.com/golang/hotime/common"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Count 计数
|
||
|
|
func (that *HoTimeDB) Count(table string, qu ...interface{}) int {
|
||
|
|
var req = []interface{}{}
|
||
|
|
if len(qu) == 2 {
|
||
|
|
req = append(req, qu[0])
|
||
|
|
req = append(req, "COUNT(*)")
|
||
|
|
req = append(req, qu[1])
|
||
|
|
} else {
|
||
|
|
req = append(req, "COUNT(*)")
|
||
|
|
req = append(req, qu...)
|
||
|
|
}
|
||
|
|
|
||
|
|
data := that.Select(table, req...)
|
||
|
|
if len(data) == 0 {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
res := ObjToStr(data[0]["COUNT(*)"])
|
||
|
|
count, _ := StrToInt(res)
|
||
|
|
return count
|
||
|
|
}
|
||
|
|
|
||
|
|
// Sum 求和
|
||
|
|
func (that *HoTimeDB) Sum(table string, column string, qu ...interface{}) float64 {
|
||
|
|
var req = []interface{}{}
|
||
|
|
if len(qu) == 2 {
|
||
|
|
req = append(req, qu[0])
|
||
|
|
req = append(req, "SUM("+column+")")
|
||
|
|
req = append(req, qu[1])
|
||
|
|
} else {
|
||
|
|
req = append(req, "SUM("+column+")")
|
||
|
|
req = append(req, qu...)
|
||
|
|
}
|
||
|
|
|
||
|
|
data := that.Select(table, req...)
|
||
|
|
if len(data) == 0 {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
res := ObjToStr(data[0]["SUM("+column+")"])
|
||
|
|
sum := ObjToFloat64(res)
|
||
|
|
return sum
|
||
|
|
}
|
||
|
|
|
||
|
|
// Avg 平均值
|
||
|
|
func (that *HoTimeDB) Avg(table string, column string, qu ...interface{}) float64 {
|
||
|
|
var req = []interface{}{}
|
||
|
|
if len(qu) == 2 {
|
||
|
|
req = append(req, qu[0])
|
||
|
|
req = append(req, "AVG("+column+")")
|
||
|
|
req = append(req, qu[1])
|
||
|
|
} else {
|
||
|
|
req = append(req, "AVG("+column+")")
|
||
|
|
req = append(req, qu...)
|
||
|
|
}
|
||
|
|
|
||
|
|
data := that.Select(table, req...)
|
||
|
|
if len(data) == 0 {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
res := ObjToStr(data[0]["AVG("+column+")"])
|
||
|
|
avg := ObjToFloat64(res)
|
||
|
|
return avg
|
||
|
|
}
|
||
|
|
|
||
|
|
// Max 最大值
|
||
|
|
func (that *HoTimeDB) Max(table string, column string, qu ...interface{}) float64 {
|
||
|
|
var req = []interface{}{}
|
||
|
|
if len(qu) == 2 {
|
||
|
|
req = append(req, qu[0])
|
||
|
|
req = append(req, "MAX("+column+")")
|
||
|
|
req = append(req, qu[1])
|
||
|
|
} else {
|
||
|
|
req = append(req, "MAX("+column+")")
|
||
|
|
req = append(req, qu...)
|
||
|
|
}
|
||
|
|
|
||
|
|
data := that.Select(table, req...)
|
||
|
|
if len(data) == 0 {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
res := ObjToStr(data[0]["MAX("+column+")"])
|
||
|
|
max := ObjToFloat64(res)
|
||
|
|
return max
|
||
|
|
}
|
||
|
|
|
||
|
|
// Min 最小值
|
||
|
|
func (that *HoTimeDB) Min(table string, column string, qu ...interface{}) float64 {
|
||
|
|
var req = []interface{}{}
|
||
|
|
if len(qu) == 2 {
|
||
|
|
req = append(req, qu[0])
|
||
|
|
req = append(req, "MIN("+column+")")
|
||
|
|
req = append(req, qu[1])
|
||
|
|
} else {
|
||
|
|
req = append(req, "MIN("+column+")")
|
||
|
|
req = append(req, qu...)
|
||
|
|
}
|
||
|
|
|
||
|
|
data := that.Select(table, req...)
|
||
|
|
if len(data) == 0 {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
res := ObjToStr(data[0]["MIN("+column+")"])
|
||
|
|
min := ObjToFloat64(res)
|
||
|
|
return min
|
||
|
|
}
|