GORM 提供了 First
、Take
、Last
方法,以便从数据库中检索单个对象。当查询数据库时它添加了 LIMIT 1
条件
// 获取第一条记录(主键升序)
db.First(&user) // SELECT * FROM `users` ORDER BY `users`.`id` LIMIT 1
// 获取第一条记录,没有指定排序字段
db.Take(&user) // SELECT * FROM `users` LIMIT 1
// 获取最后一条记录,主键降序
db.Last(&user) // SELECT * FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
result := db.Last(&user)
fmt.Println(result.RowsAffected) // 返回找到的记录数
fmt.Println(result.Error) // return error or nil
b := errors.Is(result.Error, gorm.ErrRecordNotFound) // 判断错误类型,如果是此错误类型,返回true,否则返回false
fmt.Println(b)
如果你想避免ErrRecordNotFound错误,你可以使用Find,比如db.Limit(1).Find(&user),Find方法可以接受struct和slice的数据。
First
和 Last
会根据主键排序,分别查询第一条和最后一条记录。 只有在目标 struct 是指针或者通过 db.Model()
指定 model 时,该方法才有效。 此外,如果相关 model 没有定义主键,那么将按 model 的第一个字段进行排序。
// 有效,因为目标是结构体指针
db.First(&user)
// SELECT * FROM `users` ORDER BY `users`.`id` LIMIT 1
// 有效,因为通过db.Model() 指定了model
var result map[string]interface{}
db.Model(new(User)).First(&result)
// SELECT * FROM `users` ORDER BY `users`.`id` LIMIT 1
// 无效
var result map[string]interface{}
db.Table("users").First(&result)
fmt.Println(result) // 输出:map[]
// 配合Take无效
var result map[string]interface{}
db.Table("users").Take(&result)
// 未指定主键,会根据第一个字段排序(即:`Code`)
type Language struct {
Code string
Name string
}
db.First(&Language{})
// SELECT * FROM `languages` ORDER BY `languages`.`code` LIMIT 1
用主键检错
如果主键是数字类型,您可以使用内联条件来检索对象,传入字符串参数时,需要特别注意 SQL 注入问题
db.First(&user, 2)
// SELECT * FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` LIMIT 1
db.First(&user, "2")
// SELECT * FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` LIMIT 1
db.Find(&users, []int{1, 2})
// SELECT * FROM `users` WHERE `users`.`id` IN (1,2)
如果主键是字符串(例如像 uuid),查询将被写成这样:
db.First(&user, "uuid = ?", "00000000-0000-0000-0000-000000000000")
// SELECT * FROM `users` WHERE uuid = '00000000-0000-0000-0000-000000000000' ORDER BY `users`.`id` LIMIT 1
result := db.Find(&users)
// SELECT * FROM `users`
myPrint(result.RowsAffected) // 返回的记录行数,相当于 len(users)
myPrint(result.Error) // return error
// 获取第一条匹配的记录
db.Where("name = ?", "老王").First(&user)
// SELECT * FROM `users` WHERE name = '老王' ORDER BY `users`.`id` LIMIT 1
// 获取全部匹配的记录
db.Where("name = ?", "老王").Find(&users)
// SELECT * FROM `users` WHERE name = '老王'
// IN
db.Debug().Where("age IN (?)", []uint8{10, 55}).Find(&users)
// SELECT * FROM `users` WHERE age IN (10,55)
// LIKE
db.Debug().Where("age LIKE ?", "%0%").Find(&users)
// SELECT * FROM `users` WHERE age LIKE '%0%'
// AND
db.Debug().Where("age=? AND name=?", 20, "老王").Find(&users)
_// SELECT * FROM `users` WHERE age=20 AND name='老王'
// TIME
_db.Debug().Where("created_at < ?", time.Now()).Find(&users)
// SELECT * FROM `users` WHERE created_at < '2021-11-17 13:34:51.519'
_// BETWEEN
_db.Debug().Where("age BETWEEN ? AND ?", 10, 19).Find(&users)
// SELECT * FROM `users` WHERE age BETWEEN 10 AND 19
// struct
var users []User
db.Debug().Where(&User{Name: sql.NullString{"老王", true}, Age: 20}).Find(&users)
_// SELECT * FROM `users` WHERE `users`.`name` = '老王' AND `users`.`age` = 20
// map
db.Debug().Where(map[string]interface{}{
"name": sql.NullString{"李四", true}, "age": 10,
}).First(&user)
// SELECT * FROM `users` WHERE `age` = 10 AND `name` = '李四' ORDER BY `users`.`id` LIMIT 1
// 主键切片条件
var users []User
db.Debug().Where([]uint{1, 5}).Find(&users)
// SELECT * FROM `users` WHERE `users`.`id` IN (1,5)
注意:当使用结构体作为查询条件时,GORM只会查询非零值的字段,这意味着如果您的字段为0、false、""、或其它零值时,该字段不会被用于构建查询条件,例如:
db.Debug().Where(&User{Name: sql.NullString{"老王", true}, Age: 0}).Find(&users)
// SELECT * FROM `users` WHERE `users`.`name` = '老王'
如果你想包含零值的查询条件,你可以使用map,其会包含所有key-value的查询条件
db.Debug().Where(map[string]interface{}{
"name": sql.NullString{"老王", true}, "age": 0,
}).Find(&users)
// SELECT * FROM `users` WHERE `age` = 0 AND `name` = '老王'
当使用struct进行查询时,你可以通过想where()传入struct来指定查询条件的字段、值、表名,例如
db.Debug().Where(&User{Name: sql.NullString{"老王", true}}, "name", "Age").Find(&users)
// SELECT * FROM `users` WHERE `users`.`name` = '老王' AND `users`.`age` = 0
db.Debug().Where(&User{Name: sql.NullString{"老王", true}, Age: 20}, "age").Find(&users)
// SELECT * FROM `users` WHERE `users`.`age` = 20
也就是说where里面放的结构体无论有几个字段程序不管,最终只会按照结构体后面的参数字段进行条件查询
查询条件也可以被内联到First或Find之类的方法中,其用法类似于Where
// 根据主键获取记录,如果是非整型主键
db.Debug().First(&user, "ID = ?", 5)
// SELECT * FROM `users` WHERE ID = 5 ORDER BY `users`.`id` LIMIT 1
_// plain sql
db.Debug().Find(&users, "name = ?", "老王")
// SELECT * FROM `users` WHERE name = '老王'
db.Debug().Find(&users, "name <> ? AND age = ?", "老王", 20)
// SELECT * FROM `users` WHERE name <> '老王' AND age = 20
// struct
db.Debug().Find(&users, &User{Name: sql.NullString{"赵六", true}})
// SELECT * FROM `users` WHERE `users`.`name` = '赵六'
// map
db.Debug().Find(&users, map[string]interface{}{
"age": 20,
})
// SELECT * FROM `users` WHERE `age` = 20
构建not条件,用法与Where类型
db.Debug().Not("name <> ?", "赵六").First(&user)
// SELECT * FROM `users` WHERE NOT name <> '赵六' ORDER BY `users`.`id` LIMIT 1
// Not IN
db.Debug().Not("name IN (?)", []string{"老王", "李四"}).First(&user)
// SELECT * FROM `users` WHERE NOT name IN ('老王','李四') ORDER BY `users`.`id` LIMIT 1
// map
db.Debug().Not(map[string]interface{}{
"name": []string{"老王", "李四"},
}).First(&user)
// SELECT * FROM `users` WHERE `name` NOT IN ('老王','李四') ORDER BY `users`.`id` LIMIT 1
// struct
db.Debug().Not(User{Age: uint8(10), Name: sql.NullString{"老王", true}}).Find(&users)
// SELECT * FROM `users` WHERE (`users`.`name` <> '老王' AND `users`.`age` <> 10)
// 不在主键切片中的记录
db.Debug().Not([]uint{1, 2, 3, 5}).Find(&users)
// SELECT * FROM `users` WHERE `users`.`id` NOT IN (1,2,3,5)
db.Debug().Where("name = ?", "赵六").Or("age = ?", 10).Find(&users)
// SELECT * FROM `users` WHERE name = '赵六' OR age = 10
// struct
db.Debug().Where("name = ?", "赵六").Or(&User{Age: 55}).Find(&users)
// SELECT * FROM `users` WHERE name = '赵六' OR `users`.`age` = 55
// map
db.Debug().Where("name = ?", "赵六").Or(map[string]interface{}{
"age": 55,
}).Find(&users)
// SELECT * FROM `users` WHERE name = '赵六' OR `age` = 55
Select
允许您指定从数据库中检索哪些字段, 默认情况下,GORM 会检索所有字段。
db.Debug().Select("name", "age").Find(&users)
// SELECT `name`,`age` FROM `users`
db.Debug().Select([]string{"name", "age"}).Find(&users)
// SELECT `name`,`age` FROM `users`
指定从数据库检索记录时的排序方式
db.Debug().Order("age asc, name desc").Find(&users)
// SELECT * FROM `users` ORDER BY age asc, name desc
// 多个order
db.Debug().Order("age desc").Order("name").Find(&users)
// SELECT * FROM `users` ORDER BY age desc,name
limit
指定获取记录的最大数量 Offset
指定在开始返回记录之前要跳过的记录数量
db.Debug().Limit(3).Find(&users)
// SELECT * FROM `users` LIMIT 3
// 通过-1消除limit条件
db.Debug().Limit(1).Find(&users1).Limit(-1).Find(&users2)
// SELECT * FROM `users` LIMIT 1
// SELECT * FROM `users`
_
db.Debug().Limit(1).Offset(3).Find(&users)
___// SELECT * FROM `users` LIMIT 1 OFFSET 3
// 通过-1消除Offset条件
__
db.Debug().Offset(3).Offset(-1).Find(&users)
// SELECT * FROM `users`
注意:Offset要和Limit一起使用,单独使用会报错:Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3' at line 1
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)
// SELECT name, sum(age) as total FROM `users` WHERE name LIKE "group%" GROUP BY `name` LIMIT 1
db.Model(&User{}).Select("name, sum(age) as total").Group("name").Having("name = ?", "group").Find(&result)
// SELECT name, sum(age) as total FROM `users` GROUP BY `name` HAVING name = "group"
// 智能选择字段
type APIUser struct {
Name string
Total int
}
rows, err := db.Debug().Table("users").Select("name, sum(age) as total").Group("name").Having("total > ?", 11).Rows()
// SELECT name, sum(age) as total FROM `users` GROUP BY `name` HAVING total > 11
for rows.Next() {
var users APIUser
db.ScanRows(rows, &users)
myPrint(users)
}
/*
type: main.APIUser value: {老王 65}
type: main.APIUser value: {马亚南 12}
*/
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)
db.Debug().Distinct("date", "total").Order("total desc, date desc").Find(&results)
// SELECT DISTINCT `date`,`total` FROM `results` ORDER BY total desc, date desc
db.Debug().Model(new(User)).Select("users.name, emails.email").Joins("left join emails on users.id = emails.user_id").Scan(&results)
// SELECT users.name, emails.email FROM `users` left join emails on users.id = emails.user_id
rows, err := db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Rows()
for rows.Next() {
fmt.Println(rows)
}
db.Debug().Table("users").Select("users.name, emails.email").Joins("left join emails on users.id = emails.user_id").Scan(&results)
// SELECT users.name, emails.email FROM `users` left join emails on users.id = emails.user_id
// 带参数的多表连接
// 方法一
//db.Debug().Joins("JOIN users ON users.id=emails.user_id AND users.name=?", "老王").Find(&emails)
// SELECT `emails`.`user_id`,`emails`.`email` FROM `emails` JOIN users ON users.id=emails.user_id AND users.name='老王'
// 方法二
db.Debug().Joins("JOIN users ON users.id=emails.user_id").Where("users.name=?", "李四").Find(&emails)
// SELECT `emails`.`user_id`,`emails`.`email` FROM `emails` JOIN users ON users.id=emails.user_id WHERE users.name='李四'
您可以使用 Joins
实现单条 SQL 预加载关联记录,例如:
scan将结果放到结构体中的方式与Find类似
db.Debug().Table("users").Joins("left join emails on users.id=emails.user_id").Scan(&results)
// SELECT * FROM `users` left join emails on users.id=emails.user_id
// 原生SQL
db.Debug().Raw("select name, email from users inner join emails on users.id=emails.user_id where users.name=?", "李四").Scan(&results)
// select name, email from users inner join emails on users.id=emails.user_id where users.name='李四'
手机扫一扫
移动阅读更方便
你可能感兴趣的文章