1 回答
TA贡献1856条经验 获得超11个赞
%通配符应该在字符串之内,而不是在字符串之外,即... LIKE %'foo'% ...无效,... LIKE '%foo%' ...有效。更多关于LIKE。
func (f *UserFilter) ListAllUsers(srch string) (cnt int64, l []*ListResp, err error) {
o := orm.NewOrm()
var args []interface{}
var w string
q := `SELECT * FROM users WHERE 1 = 1`
if srch != "" {
q += ` AND (LOWER(first_name) LIKE ? OR LOWER(last_name) LIKE ? OR id = ?)`
args = append(args, "%"+srch+"%", "%"+srch+"%", srch)
}
_, err = o.Raw(q, args...).QueryRows(&l)
return
}
或使用mysql的CONCAT:
func (f *UserFilter) ListAllUsers(srch string) (cnt int64, l []*ListResp, err error) {
o := orm.NewOrm()
var args []interface{}
var w string
q := `SELECT * FROM users WHERE 1 = 1`
if srch != "" {
q += ` AND (LOWER(first_name) LIKE CONCAT('%', ?, '%') OR LOWER(last_name) LIKE CONCAT('%', ?, '%') OR id = ?)`
args = append(args, srch, srch, srch)
}
_, err = o.Raw(q, args...).QueryRows(&l)
return
}
- 1 回答
- 0 关注
- 221 浏览
添加回答
举报