gorm之使用count统计满足某一条件的记录个数
package mainimport ("fmt"_ "github.com/go-sql-driver/mysql""github.com/jinzhu/gorm")var DB *gorm.DBfunc init() {var err errorDB, err = gorm.Open("mysql", "root:123456@tcp(192.168.8...
·
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
)
var DB *gorm.DB
func init() {
var err error
DB, err = gorm.Open("mysql", "root:123456@tcp(192.168.88.130:3306)/test?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
DB.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户'").AutoMigrate(&UserInfo{})
}
type UserInfo struct {
Name string `orm:"pk;column(name)"`
Password string
}
func main(){
var count int
name非daw的记录个数
DB.Model(&UserInfo{}).Where("name <> ?","daw").Count(&count)
fmt.Println(count)
}
更多推荐
已为社区贡献1条内容
所有评论(0)