o:=orm.NewOrm()varuserUserinfoqs:=o.QueryTable(user)// 返回 QuerySeterqs.Filter("id",3)// WHERE id = 1qs.Filter("username","xxx2")// WHERE username=xxx2qs.Filter("profile__age__in",18,20)// // WHERE profile.age IN (18, 20)qs.Filter("profile__age__in",18,20).Exclude("profile__lt",1000)// // WHERE profile.age IN (18, 20) AND NOT profile_id < 1000
获取多条数据
例子1,根据条件age>17,获取20位置开始的10条数据的数据
1
2
3
varallusers[]Userqs.Filter("profile__age__gt",17)// // WHERE profile.age > 17
ifnum,err:=o.Delete(&Userinfo{Uid:1});err==nil{fmt.Println(num)}//Delete 操作会对反向关系进行操作,此例中 Post 拥有一个到 User 的外键。删除 User 的时候。// 如果 on_delete 设置为默认的级联操作,将删除对应的 Post
qs.OrderBy("id","-profile__age")// ORDER BY id ASC, profile.age DESCqs.OrderBy("-profile__age","profile")// ORDER BY profile.age DESC, profile_id ASC// 上面的代码中出现了两个新接口函数// GroupBy:用来指定进行groupby的字段// Having:用来指定having执行的时候的条件
最后,使用原始sql命令:
简单示例:
1
2
3
o:=orm.NewOrm()varrorm.RawSeterr=o.Raw("UPDATE user SET name = ? WHERE name = ?","testing","xxx2")
复杂的原生sql使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func(m*User)Query(namestring)user[]User{varoorm.Ormervarrsorm.RawSetero=orm.NewOrm()rs=o.Raw("SELECT * FROM user"+"WHERE name=? AND uid>10 "+"ORDER BY uid DESC "+"LIMIT 100",name)//var user []Usernum,err:=rs.QueryRows(&user)iferr!=nil{fmt.Println(err)}else{fmt.Println(num)//return user}return}