例如:
$User = M("User"); // 实例化User对象 $User->where('type=1 AND status=1')->select();
SELECT * FROM think_user WHERE type=1 AND status=1
如果进行多字段查询,那么字段之间的默认逻辑关系是 逻辑与 AND,但是用下面的规则可以更改默认的逻辑判断,通过使用 _logic 定义查询逻辑:
$User = M("User"); // 实例化User对象 $condition['name'] = 'thinkphp'; $condition['account'] = 'thinkphp'; $condition['_logic'] = 'OR'; // 把查询条件传入查询方法 $User->where($condition)->select();
SELECT * FROM think_user WHERE `name`='thinkphp' OR `account`='thinkphp'
二、数组方式作为查询条件
讲了这么多了like查询怎么实现呢,下面看
$userForm=M('user'); $where['name']=array('like','jb51%'); $userForm->where($where)->select();
name like 'jb51%'
查询语句:
$where['name']=array('like',array('%jb51%','%.com'),'OR');
name like '%jb51%' or name like '%.com'
查询语句:
$where['name']=array(array('like','%a%'),array('like','%b%'),array('like','%c%'),'jb51','or');
(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'jb51')
查询语句:
$where['_string']='(name like "%jb51%") OR (title like "%jb51")';
name like '%jb51%' or title like '%jb51'
希望本文所述对大家的PHP程序设计有所帮助。