or 条件
// Struct
db.Where("name='jinzhu'").Or(User{Name:"jinzhu2", Age:18}).Find(&users);
// select * from users where name='jinzhu' or(name='jinzhu2 and age=18);
子查询
子查询可以嵌套在查询中,GORM允许在使用 *gorm.DB对象作为参数时生成子查询
db.Where("amount > (?)", db.Table("orders").Select("AVG(amount)")).Find(&orders)
//select * from "orders" where amount > (select AVG(amount) from "orders");
subQuery := db.Select("AVG(age)").Where("name Like ?", "name%").Table("users")
db.Select("AVG(age) as avgage").Group("name").Having("AVG(age) > (?)", subQuery).Find(&results);
//select avg(age) as avgage from users group by name having avg(age) > (select avg(age) from users where name like "name%")
db.Not("name = ?", "jinzhu").First(&user)
// select * from users where not name = "ninzhu" order by id limit 1;
// Not In
db.NotIn(map[string]interface{}{"name":[]string{"jinzhu", "jinzhu2"}}).Find(&users)
// select * from users where name not in ("jinzhu, "jinzhu2");
// Struct
db.Not(User{Name:"jinzhu", Age:18}).First(&user);
// select * from users where name <> "jinzhu" and age <> 18 order by id limit 1;