本篇文章IT兄弟连GO语言学院小美
给读者们分享一下Beego框架:高级查询
对GO语言感兴趣想要学习Golang开发技术的小伙伴就随小编来了解一下吧。
isnull:判断某个字段是否为null
```
func (this *OperatorsController) GetIsNull() {
user := models.User{}
var users []*models.User
orm := orm.NewOrm()//创建orm
qs := orm.QueryTable(user)//创建句柄
n, err := qs.Filter("name__isnull", false).All(&users)
if err != nil {
this.Ctx.WriteString("查询失败!")
return
}
fmt.Println("n = ", n)
for _, u := range users {
fmt.Println("u = ", u)
}
this.Ctx.WriteString("查询成功!")
}
```
in:和sql中得in含义相同,通常和Filter结合使用
```
func (this *OperatorsController) GetIn() {
user := models.User{}
var users []*models.User
//创建orm
orm := orm.NewOrm()
//创建句柄
qs := orm.QueryTable(user)
//SELECT * FROM USER WHERE id IN(2, 5, 6);
/*buffer := []int{ 13, 14, 15, 16}
n, err := qs.Filter("id__in", buffer).All(&users)*/
n, err := qs.Filter("id__in", 13, 14, 15, 16).All(&users)
if err != nil {
this.Ctx.WriteString("查询失败!")
return
}
fmt.Println("n = ", n)
for _, u := range users {
fmt.Println("u = ", u)
}
this.Ctx.WriteString("查询成功!")
}
```
istartswith:判断某个字段是否以指定内容开始
```
func (this *OperatorsController) GetStartswith() {
user := models.User{}
var users []*models.User
//创建orm
orm := orm.NewOrm()
//创建句柄
qs := orm.QueryTable(user)
//SELECT * FROM USER WHERE NAME LIKE '杜%'
n, err := qs.Filter("name__startswith", "B").All(&users)
if err != nil {
this.Ctx.WriteString("查询失败!")
return
}
fmt.Println("n = ", n)
for _, u := range users {
fmt.Println("u = ", u)
}
this.Ctx.WriteString("查询成功!")
}
```
gt:大于,通常和Filter结合使用
```
func (this *OperatorsController) GetGt() {
user := models.User{}
var users []*models.User
//创建orm
orm := orm.NewOrm()
//创建句柄
qs := orm.QueryTable(user)
//SELECT * FROM USER WHERE id > 2;
n, err := qs.Filter("id__gt", 14).All(&users)
if err != nil {
this.Ctx.WriteString("查询失败!")
return
}
fmt.Println("n = ", n)
for _, u := range users {
fmt.Println("u = ", u)
}
this.Ctx.WriteString("查询成功!")
}
```
contains:判断某个字段是否 包含指定字段
```
func (this *OperatorsController) GetContains() {
user := models.User{}
//创建用户指针切片,用于存储查询结果
var users []*models.User
//创建orm
orm := orm.NewOrm()
//创建句柄
qs := orm.QueryTable(user)
//SELECT * FROM USER WHERE NAME LIKE '%紫%';
n, err := qs.Filter("name__contains", "o").All(&users)
if err != nil {
this.Ctx.WriteString("查询失败!")
return
}
fmt.Println("n = ", n)
for _, u := range users {
fmt.Println("u = ", u)
}
this.Ctx.WriteString("查询成功!")
}
```
exact:对某个条件取反
```
func (this *OperatorsController) GetExact() {
user := models.User{}
//创建orm
orm := orm.NewOrm()
//创建句柄
qs := orm.QueryTable("user")
//SELECT * FROM USER WHERE id = 7;
//在user表中查询id等于12的用户
err := qs.Filter("id__exact", 12).One(&user)
if err != nil {
this.Ctx.WriteString("查询失败!")
return
}
fmt.Println("user = ", user)
this.Ctx.WriteString("查询成功!")
}
```
想要了解更多关于GO语言开发方面内容的小伙伴,
请关注IT兄弟连官网、公众号:GO语言研习社,
IT兄弟连教育有专业的微软、谷歌讲师为您指导,
此外IT兄弟连老师精心推出的GO语言教程定能让你快速掌握GO语言从入门到精通开发实战技能。