现有一个用户表,然后查询用户的时候需要查询用户名或用户昵称、用户级别三个值来筛选用户。
所以这里需要的是where条件语句
一般解法
$where = "( username like %$username% OR nickname like %$nickname% ) AND usertype = $usertype ";
但是这里如果用ThinkPHP来做的话,该怎么写呢?
ThinkPHP写法
$where['username'] = array('like','%'.$username.'%'); $where['nickname'] = array('like','%'.$nickname.'%'); $where['_logic'] = 'or'; $map['_complex'] = $where; $map['usertype'] = array('eq',$usertype);
生成的sql语句是
( username like %$username% OR nickname like %$nickname% ) AND (usertype = $usertype ")
ThinkPHP手册链接:http://www.kancloud.cn/manual/thinkphp/1771
上一篇: 手把手安装青春博客2.0...
下一篇: TP下联合多表查询...