where方法的用法是ThinkPHP查询语言的精髓,也是ThinkPHP ORM的重要组成部分和亮点所在,可以完成包括普通查询、表达式查询、快捷查询、区间查询、组合查询在内的查询操作。where方法的参数支持字符串和数组,虽然也可以使用对象但并不建议。
字符串条件$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查询方法。
更多关于云服务器,域名注册,虚拟主机的问题,请访问西部数码官网:www.west.cn