Skip to content

条件查询


Where()WhereAnd()

可通过链式调用的方式来指定多条件的查询,但要注意 WhereAnd() 前需要有一个 Where()

  • @param field 字段名
  • @param condition 条件
  • @param value

单条件查询 Where()

var user []User
err := tg.Db("user").Where("age", ">", 10).Select(&user)
var user []User
err := tg.Db("user").Where("age", ">", 10).Select(&user)

多条件查询 WhereAnd()

var user []User
err := tg.Db("user").Where("age", ">", 10).WhereAnd("gender", "=", 1).WhereAnd("status", "=", 2).Select(&user)
var user []User
err := tg.Db("user").Where("age", ">", 10).WhereAnd("gender", "=", 1).WhereAnd("status", "=", 2).Select(&user)

最终的SQL语句为:

SELECT * FROM user WHERE age = 10 AND gender = 1 AND status = 2
SELECT * FROM user WHERE age = 10 AND gender = 1 AND status = 2

条件对照表 condition


条件说明
=相等
!=不等于
<>不等于
>大于
>=大于等于
<小于
<=小于等于

备案号:冀ICP备20015584号-5