高级查询
测试数据在我的gitee里面blogs.sql
https://gitee.com/wangfanbest/gin-blog
分组统计查询
按照年月分组查博客条数
// 分组查询
func GroupSelect() {
var results []map[string]interface{}
db.Table("blogs").Select("group_concat(id)", "count(id) as num", "date_format(created_at,'%Y-%m') as ym").Group("ym").Find(&results)
fmt.Println(results)
}
in
func YmSelect() {
var results []map[string]interface{}
db.Table("blogs").Select("id", "name").Where("id IN ?", []uint{3, 5}).Find(&results)
fmt.Println(results)
}
between and
func YmSelect() {
var results []map[string]interface{}
db.Table("blogs").Select("id", "name").Where("created_at BETWEEN ? AND ?", "2022-03-01 00:00:00", "2022-03-31 00:00:00").Find(&results)
fmt.Println(results)
}
时间查询
func YmSelect() {
var results []map[string]interface{}
db.Table("blogs").Select("id", "name").Where("date_format(created_at,'%Y-%m') = ?", "2022-03").Find(&results)
fmt.Println(results)
}