in查询

使用字符串、slice参数类型。当使用slice参数类型时,预处理占位符只需要一个?即可。

  1. // SELECT * FROM user WHERE uid IN(100,10000,90000)
  2. db.Model("user").Where("uid IN(?,?,?)", 100, 10000, 90000).All()
  3. db.Model("user").Where("uid", g.Slice{100, 10000, 90000}).All()
  4. // SELECT * FROM user WHERE gender=1 AND uid IN(100,10000,90000)
  5. db.Model("user").Where("gender=? AND uid IN(?)", 1, g.Slice{100, 10000, 90000}).All()
  6. // SELECT COUNT(*) FROM user WHERE age in(18,50)
  7. db.Model("user").Where("age IN(?,?)", 18, 50).Count()
  8. db.Model("user").Where("age", g.Slice{18, 50}).Count()

使用任意map参数类型。

  1. // SELECT * FROM user WHERE gender=1 AND uid IN(100,10000,90000)
  2. db.Model("user").Where(g.Map{
  3. "gender" : 1,
  4. "uid" : g.Slice{100,10000,90000},
  5. }).All()

使用struct参数类型,注意查询条件的顺序和struct的属性定义顺序有关。

  1. type User struct {
  2. Id []int `orm:"uid"`
  3. Gender int `orm:"gender"`
  4. }
  5. // SELECT * FROM `user` WHERE uid IN(100,10000,90000) AND gender=1
  6. db.Model("user").Where(User{
  7. "gender" : 1,
  8. "uid" : []int{100, 10000, 90000},
  9. }).All()

为提高易用性,当传递的slice参数为空或nil时,查询并不会报错,而是转换为一个false条件语句。

  1. // SELECT * FROM `user` WHERE 0=1
  2. db.Model("user").Where("uid", g.Slice{}).All()
  3. db.Model("user").Where("uid", nil).All()

goframe v1.16版本开始,goframeORM同时也提供了常用条件方法WhereIn/WhereNotIn/WhereOrIn/WhereOrNotIn方法,用于常用的In查询条件过滤。方法定义如下:

  1. func (m *Model) WhereIn(column string, in interface{}) *Model
  2. func (m *Model) WhereNotIn(column string, in interface{}) *Model
  3. func (m *Model) WhereOrIn(column string, in interface{}) *Model
  4. func (m *Model) WhereOrNotIn(column string, in interface{}) *Model

使用示例:

  1. // SELECT * FROM `user` WHERE `gender`=1 AND `type` IN(1,2,3)
  2. db.Model("user").Where("gender", 1).WhereIn("type", g.Slice{1,2,3}).All()
  3. // SELECT * FROM `user` WHERE `gender`=1 AND `type` NOT IN(1,2,3)
  4. db.Model("user").Where("gender", 1).WhereNotIn("type", g.Slice{1,2,3}).All()
  5. // SELECT * FROM `user` WHERE `gender`=1 OR `type` IN(1,2,3)
  6. db.Model("user").Where("gender", 1).WhereOrIn("type", g.Slice{1,2,3}).All()
  7. // SELECT * FROM `user` WHERE `gender`=1 OR `type` NOT IN(1,2,3)
  8. db.Model("user").Where("gender", 1).WhereOrNotIn("type", g.Slice{1,2,3}).All()

like 查询

  1. // SELECT * FROM `user` WHERE name like '%john%'
  2. db.Model("user").Where("name like ?", "%john%").All()
  3. // SELECT * FROM `user` WHERE birthday like '1990-%'
  4. db.Model("user").Where("birthday like ?", "1990-%").All()

goframe v1.16版本开始,goframeORM同时也提供了常用条件方法WhereLike/WhereNotLike/WhereOrLike/WhereOrNotLike方法,用于常用的Like查询条件过滤。方法定义如下:

  1. func (m *Model) WhereLike(column string, like interface{}) *Model
  2. func (m *Model) WhereNotLike(column string, like interface{}) *Model
  3. func (m *Model) WhereOrLike(column string, like interface{}) *Model
  4. func (m *Model) WhereOrNotLike(column string, like interface{}) *Model

使用示例:

  1. // SELECT * FROM `user` WHERE `gender`=1 AND `name` LIKE 'john%'
  2. db.Model("user").Where("gender", 1).WhereLike("name", "john%").All()
  3. // SELECT * FROM `user` WHERE `gender`=1 AND `name` NOT LIKE 'john%'
  4. db.Model("user").Where("gender", 1).WhereNotLike("name", "john%").All()
  5. // SELECT * FROM `user` WHERE `gender`=1 OR `name` LIKE 'john%'
  6. db.Model("user").Where("gender", 1).WhereOrLike("name", "john%").All()
  7. // SELECT * FROM `user` WHERE `gender`=1 OR `name` NOT LIKE 'john%'
  8. db.Model("user").Where("gender", 1).WhereOrNotLike("name", "john%").All()

min/max/avg/sum

我们直接将统计方法使用在Fields方法上,例如:

  1. // SELECT MIN(score) FROM `user` WHERE `uid`=1
  2. db.Model("user").Fields("MIN(score)").Where("uid", 1).Value()
  3. // SELECT MAX(score) FROM `user` WHERE `uid`=1
  4. db.Model("user").Fields("MAX(score)").Where("uid", 1).Value()
  5. // SELECT AVG(score) FROM `user` WHERE `uid`=1
  6. db.Model("user").Fields("AVG(score)").Where("uid", 1).Value()
  7. // SELECT SUM(score) FROM `user` WHERE `uid`=1
  8. db.Model("user").Fields("SUM(score)").Where("uid", 1).Value()

goframe v1.16版本开始,goframeORM同时也提供了常用统计方法Min/Max/Avg/Sum方法,用于常用的字段统计查询。方法定义如下:

  1. func (m *Model) Min(column string) (float64, error)
  2. func (m *Model) Max(column string) (float64, error)
  3. func (m *Model) Avg(column string) (float64, error)
  4. func (m *Model) Sum(column string) (float64, error)

上面的示例使用快捷统计方法改造后:

  1. // SELECT MIN(`score`) FROM `user` WHERE `uid`=1
  2. db.Model("user").Where("uid", 1).Min("score")
  3. // SELECT MAX(`score`) FROM `user` WHERE `uid`=1
  4. db.Model("user").Where("uid", 1).Max("score")
  5. // SELECT AVG(`score`) FROM `user` WHERE `uid`=1
  6. db.Model("user").Where("uid", 1).Avg("score")
  7. // SELECT SUM(`score`) FROM `user` WHERE `uid`=1
  8. db.Model("user").Where("uid", 1).Sum("score")

count查询

  1. // SELECT COUNT(1) FROM `user` WHERE `birthday`='1990-10-01'
  2. db.Model("user").Where("birthday", "1990-10-01").Count()
  3. // SELECT COUNT(uid) FROM `user` WHERE `birthday`='1990-10-01'
  4. db.Model("user").Fields("uid").Where("birthday", "1990-10-01").Count()

goframe v1.16版本开始,goframeORM同时也提供了一个按照字段进行Count的常用方法CountColumn。方法定义如下:

  1. func (m *Model) CountColumn(column string) (int, error)

使用示例:

  1. db.Model("user").Where("birthday", "1990-10-01").CountColumn("uid")

distinct查询

  1. // SELECT DISTINCT uid,name FROM `user`
  2. db.Model("user").Fields("DISTINCT uid,name").All()
  3. // SELECT COUNT(DISTINCT uid,name) FROM `user`
  4. db.Model("user").Fields("DISTINCT uid,name").Count()

goframe v1.16版本开始,goframeORM同时也提供了一个字段唯一性过滤标记方法Distinct。方法定义如下:

  1. func (m *Model) Distinct() *Model

使用示例:

  1. // SELECT COUNT(DISTINCT `name`) FROM `user`
  2. db.Model("user").Distinct().CountColumn("name")
  3. // SELECT COUNT(DISTINCT uid,name) FROM `user`
  4. db.Model("user").Distinct().CountColumn("uid,name")

between查询

  1. // SELECT * FROM `user ` WHERE age between 18 and 20
  2. db.Model("user").Where("age between ? and ?", 18, 20).All()

goframe v1.16版本开始,goframeORM同时也提供了常用条件方法WhereBetween/WhereNotBetween/WhereOrBetween/WhereOrNotBetween方法,用于常用的Between查询条件过滤。方法定义如下:

  1. func (m *Model) WhereBetween(column string, min, max interface{}) *Model
  2. func (m *Model) WhereNotBetween(column string, min, max interface{}) *Model
  3. func (m *Model) WhereOrBetween(column string, min, max interface{}) *Model
  4. func (m *Model) WhereOrNotBetween(column string, min, max interface{}) *Model

使用示例:

  1. // SELECT * FROM `user` WHERE `gender`=0 AND `age` BETWEEN 16 AND 20
  2. db.Model("user").Where("gender", 0).WhereBetween("age", 16, 20).All()
  3. // SELECT * FROM `user` WHERE `gender`=0 AND `age` NOT BETWEEN 16 AND 20
  4. db.Model("user").Where("gender", 0).WhereNotBetween("age", 16, 20).All()
  5. // SELECT * FROM `user` WHERE `gender`=0 OR `age` BETWEEN 16 AND 20
  6. db.Model("user").Where("gender", 0).WhereOrBetween("age", 16, 20).All()
  7. // SELECT * FROM `user` WHERE `gender`=0 OR `age` NOT BETWEEN 16 AND 20
  8. db.Model("user").Where("gender", 0).WhereOrNotBetween("age", 16, 20).All()

null查询

goframe v1.16版本开始,goframeORM提供了常用条件方法WhereNull/WhereNotNull/WhereOrNull/WhereOrNotNull方法,用于常用的Null查询条件过滤。方法定义如下:

  1. func (m *Model) WhereNull(columns ...string) *Model
  2. func (m *Model) WhereNotNull(columns ...string) *Model
  3. func (m *Model) WhereOrNull(columns ...string) *Model
  4. func (m *Model) WhereOrNotNull(columns ...string) *Model

使用示例:

  1. // SELECT * FROM `user` WHERE `created_at` > '2021-05-01 00:00:00' AND `inviter` IS NULL
  2. db.Model("user").Where("created_at>?", gtime.New("2021-05-01")).WhereNull("inviter").All()
  3. // SELECT * FROM `user` WHERE `created_at` > '2021-05-01 00:00:00' AND `inviter` IS NOT NULL
  4. db.Model("user").Where("created_at>?", gtime.New("2021-05-01")).WhereNotNull("inviter").All()
  5. // SELECT * FROM `user` WHERE `created_at` > '2021-05-01 00:00:00' OR `inviter` IS NULL
  6. db.Model("user").Where("created_at>?", gtime.New("2021-05-01")).WhereOrNull("inviter").All()
  7. // SELECT * FROM `user` WHERE `created_at` > '2021-05-01 00:00:00' OR `inviter` IS NOT NULL
  8. db.Model("user").Where("created_at>?", gtime.New("2021-05-01")).WhereOrNotNull("inviter").All()

同时,这几个方法的参数支持多个字段输入,例如:

  1. // SELECT * FROM `user` WHERE `created_at` > '2021-05-01 00:00:00' AND `inviter` IS NULL AND `creator` IS NULL
  2. db.Model("user").Where("created_at>?", gtime.New("2021-05-01")).WhereNull("inviter", "creator").All()

Content Menu