我需要分开where
以有条件地验证值,示例
email := "test@test.com"
if email != "" {
db.Where("users.email LIKE ?", "%"+email+"%")
}
db.Where("users.name like ?", "%"+jhon+"%").Find(&users)
这将返回两个查询:
1:SELECT "users".* FROM "users" WHERE users.email LIKE '%test@test.com%'
2:SELECT "users".* FROM "users" WHERE users.name LIKE '%jhon%'
但我只需要一个查询的结果:
SELECT "users".* FROM "users" WHERE users.email LIKE '%test@test.com%' and users.name LIKE '%jhon%'
谢谢!