GORM查询
1. GORM基本查询
1.1. 基本查询
func (c *UserController) UserSelect() {
user := models.User{Id: 3}
models.DB.Find(&user)
user := []models.User{}
result := models.DB.Find(&user)
fmt.Println(result.RowsAffected )
users := models.User{}
models.DB.First(&users)
c.Data["json"] = user
c.ServeJSON()
}
1.2. 用主键检索
func (c *UserController) UserSelect() {
user := []models.User{}
models.DB.First(&user, 2)
models.DB.Find(&user, []int{1,2,3})
c.Data["json"] = user
c.ServeJSON()
}
2. 条件查询
2.1. String 条件
func (c *UserController) UserSelect() {
user := []models.User{}
models.DB.Where("username = ?", "zhangsan").First(&user)
models.DB.Where("username = ?", "zhangsan").Find(&user)
models.DB.Where("username <> ?", "jinzhu").Find(&user)
models.DB.Where("username IN (?)", []string{"jinzhu", "jinzhu 2"}).Find(&user)
models.DB.Where("username LIKE ?", "%jin%").Find(&user)
models.DB.Where("username = ? AND age >= ?", "jinzhu", "22").Find(&user)
models.DB.Where("updated_at > ?", lastWeek).Find(&user)
models.DB.Where("created_at BETWEEN ? AND ?", lastWeek, today).Find(&user)
c.Data["json"] = user
c.ServeJSON()
}
2.2. Struch & Map 查询
func (c *UserController) UserSelect() {
user := []models.User{}
models.DB.Where(&models.User{Username: "zhangsan", Age: 26}).First(&user)
models.DB.Where(map[string]interface{}{"username": "zhangsan", "age": 26}).Find(&user)
models.DB.Where([]int64{1, 2, 3}).Find(&user)
c.Data["json"] = user
c.ServeJSON()
}
type User struct {
Id int
Username string
Age int
Email string
AddTime int
}
2.3. Not条件
func (c *UserController) UserSelect() {
user := []models.User{}
models.DB.Not("zhangsan", "lisi").First(&user)
models.DB.Not("username", []string{"zhangsan", "lisi"}).Find(&user)
models.DB.Not([]int64{1,2,3}).First(&user)
models.DB.Not("name = ?", "jinzhu").First(&user)
models.DB.Not(models.User{Username: "jinzhu"}).First(&user)
c.Data["json"] = user
c.ServeJSON()
}
2.4. Or条件
func (c *UserController) UserSelect() {
user := []models.User{}
models.DB.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&user)
models.DB.Where("name = 'jinzhu'").Or(models.User{Username: "jinzhu 2"}).Find(&user)
models.DB.Where("name = 'jinzhu'").Or(map[string]interface{}{"name": "jinzhu 2"}).Find(&user)
c.Data["json"] = user
c.ServeJSON()
}
3. 高级查询
3.1. 选择特定字段
- 选择您想从数据库中检索的字段,默认情况下会选择全部字段
db.Select("name", "age").Find(&users)
db.Select([]string{"name", "age"}).Find(&users)
db.Table("users").Select("COALESCE(age,?)", 42).Rows()
3.2. Order
db.Order("age desc, name").Find(&users)
db.Order("age desc").Order("name").Find(&users)
db.Clauses(clause.OrderBy{
Expression: clause.Expr{SQL: "FIELD(id,?)", Vars: []interface{}{[]int{1, 2, 3}}, WithoutParentheses: true},
}).Find(&User{})
3.3. Limit & Offset
db.Limit(3).Find(&users)
db.Limit(10).Find(&users1).Limit(-1).Find(&users2)
db.Offset(3).Find(&users)
db.Limit(10).Offset(5).Find(&users)
db.Offset(10).Find(&users1).Offset(-1).Find(&users2)
3.4. Group & Having
type result struct {
Date time.Time
Total int
}
db.Model(&User{}).Select("name, sum(age) as total").Where("name LIKE ?", "group%").Group("name").First(&result)
db.Model(&User{}).Select("name, sum(age) as total").Group("name").Having("name = ?", "group").Find(&result)
rows, err := db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Rows()
for rows.Next() {
...
}
rows, err := db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Having("sum(amount) > ?", 100).Rows()
for rows.Next() {
...
}
type Result struct {
Date time.Time
Total int64
}
db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Having("sum(amount) > ?", 100).Scan(&results)
3.5. Distinct
db.Distinct("name", "age").Order("name, age desc").Find(&results)
4. 连表查询
4.1. Joins
type result struct {
Name string
Email string
}
db.Model(&User{}).Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Scan(&result{})
rows, err := db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Rows()
for rows.Next() {
...
}
db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Scan(&results)
db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Joins("JOIN credit_cards ON credit_cards.user_id = users.id").Where("credit_cards.number = ?", "411111111111").Find(&user)
4.2. Joins 预加载
- 您可以使用
Joins
实现单条 SQL 预加载关联记录,例如:
db.Joins("Company").Find(&users)