连贯查询之多条件查询where

连贯查询where默认and:

$m_xinxianshi = M('Xinxianshi');
                    // 通用查询条件
                    // $cond['is_deleted'] = '0';
                    //$Model->join('t2 on t1.id = t2.uid', 'left')->join('t3 on t2.uid = t3.sid', 'left')->select();
                    $cond['lb_xinxianshi.beipinglunid'] = '0';
                    $cond['lb_xinxianshi.xianshi'] = '0';
                    $xinxianshi_list = $m_xinxianshi
                    ->table('lb_xinxianshi')
                    ->join("LEFT JOIN lb_user ON lb_xinxianshi.openid = lb_user.openid")
                    ->join("LEFT JOIN lb_shequ ON lb_xinxianshi.shequid = lb_shequ.id")
                    ->field('lb_xinxianshi.*,lb_user.name as name,lb_shequ.mingcheng as mingcheng')
                    ->where($cond)
                    ->order('id desc')
                    ->page($page, $pagesize)
                    ->group('id')
                    ->select();

SELECT lb_xinxianshi.*,lb_user.name as name,lb_shequ.mingcheng as mingcheng FROM `lb_xinxianshi` LEFT JOIN lb_user ON lb_xinxianshi.openid = lb_user.openid LEFT JOIN lb_shequ ON lb_xinxianshi.shequid = lb_shequ.id WHERE lb_xinxianshi.beipinglunid = '0' AND lb_xinxianshi.xianshi = '0' GROUP BY id ORDER BY id desc LIMIT 0,10
如要改成查询条件为OR:代码改为:

$m_xinxianshi = M('Xinxianshi');
                    // 通用查询条件
                    // $cond['is_deleted'] = '0';
                    //$Model->join('t2 on t1.id = t2.uid', 'left')->join('t3 on t2.uid = t3.sid', 'left')->select();
                    $cond['lb_xinxianshi.beipinglunid'] = '0';
                    $cond['lb_xinxianshi.xianshi'] = '0';
                    $cond['_logic'] = 'or';
                    $xinxianshi_list = $m_xinxianshi
                    ->table('lb_xinxianshi')
                    ->join("LEFT JOIN lb_user ON lb_xinxianshi.openid = lb_user.openid")
                    ->join("LEFT JOIN lb_shequ ON lb_xinxianshi.shequid = lb_shequ.id")
                    ->field('lb_xinxianshi.*,lb_user.name as name,lb_shequ.mingcheng as mingcheng')
                    ->where($cond)
                    ->order('id desc')
                    ->page($page, $pagesize)
                    ->group('id')
                    ->select();

即:$cond['lb_xinxianshi.beipinglunid'] = '0'; $cond['lb_xinxianshi.xianshi'] = '0'; $cond['_logic'] = 'or';
复合查询相当于封装了一个新的查询条件,然后并入原来的查询条件之中,所以可以完成比较复杂的查询条件组装。 例如:

$where['name']  = array('like', '%thinkphp%');
$where['title']  = array('like','%thinkphp%');
$where['_logic'] = 'or';
$map['_complex'] = $where;
$map['id']  = array('gt',1);

SQL:

( id > 1) AND ( ( name like '%thinkphp%') OR ( title like '%thinkphp%') )

你可能感兴趣的:(连贯查询之多条件查询where)