字符串条件
$User = M("User"); // 实例化User对象$User->where('type=1 AND status=1')->select();
SELECT * FROM think_user WHERE type=1 AND status=1
数组条件普通查询$User = M("User"); // 实例化User对象 $map['name'] = 'thinkphp'; $map['status'] = 1; // 把查询条件传入查询方法 $User->where($map)->select();
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
表达式查询$map['字段1'] = array('表达式','查询条件1'); $map['字段2'] = array('表达式','查询条件2'); $Model->where($map)->select(); // 也支持
$map['id'] = array('eq',100);
表示的查询条件就是 id = 100
$map['id'] = array('neq',100);
表示的查询条件就是 id <> 100
$map['id'] = array('gt',100);
表示的查询条件就是 id > 100
$map['id'] = array('egt',100);
表示的查询条件就是 id >= 100
$map['id'] = array('lt',100);
表示的查询条件就是 id < 100
$map['id'] = array('elt',100);
表示的查询条件就是 id <= 100
[NOT] LIKE: 同sql的LIKE
$map['name'] = array('like','thinkphp%');
查询条件就变成 name like 'thinkphp%'
$map['a'] =array('like',array('%thinkphp%','%tp'),'OR'); $map['b'] =array('notlike',array('%thinkphp%','%tp'),'AND');
生成的查询条件就是:(a like '%thinkphp%' OR a like '%tp') AND (b not like '%thinkphp%' AND b not like '%tp')
[NOT] BETWEEN :同sql的[not] between, 查询条件支持字符串或者数组,例如:
$map['id'] = array('between','1,8');
$map['id'] = array('between',array('1','8'));
[NOT] IN: 同sql的[not] in ,查询条件支持字符串或者数组,例如:
$map['id'] = array('not in','1,5,8');
$map['id'] = array('not in',array('1','5','8'));
EXP:表达式,支持更复杂的查询情况
$map['id'] = array('exp',' IN (1,3,8) ');
等同于
$map['id'] = array('in','1,3,8');组合查询
$User = M("User"); // 实例化User对象 $map['id'] = array('neq',1);$map['name'] = 'ok'; $map['_string'] = 'status=1 AND score>10'; $User->where($map)->select();
最后得到的查询条件就成了:( `id` != 1 ) AND ( `name` = 'ok' ) AND ( status=1 AND score>10 )
复合查询
$where['name'] = array('like', '%thinkphp%'); $where['title'] = array('like','%thinkphp%'); $where['_logic'] = 'or'; $map['_complex'] = $where;$map['id'] = array('gt',1);
等同于
$where['id'] = array('gt',1); $where['_string'] = ' (name like "%thinkphp%") OR ( title like "%thinkphp") ';
查询条件是
( id > 1) AND ( ( name like '%thinkphp%') OR ( title like '%thinkphp%') )
等等这些都是常用的where查询方法。
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/4994302.html,如需转载请自行联系原作者
以上就是thinkphp重点方法详解之where()方法的详细内容,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)