## 條件查詢方法
### `where`方法
~~~
db()->table('industry')->where('slug=? ',[$data['slug']])->one();
~~~
在我們的查詢過程中,有時候會遇到`in`查詢,使用方法如下:
~~~
$in=[1,2,3,4];
$e=[3,4,5];
if($in){
$all=db()->table('stores_users_info')->where('stores_id=? and stores_role_id in ('.DB::in($e).')',$in)->all();
}else{
$all=array();
}
~~~
>[info]注意$e和$in不能為空,否則會報錯。
在我們進行搜索操作的時候往往會有更多中查詢的結構出現,下面我將介紹一下在后臺頁面分頁搜索的時候如何編寫條件進行查詢操作:
一個簡單的門店管理列表頁:
~~~
//門店管理
static function pager_stores(){
$where = ['page'=>$_GET['size']?:10,'url'=>'dianxiaoer/stores/index'];
$q = $_GET['user'];
if($_GET['uid']){
$where['uid']=$_GET['uid'];
}
if($q){
$where['and_where'][] = ['(id=? or name=?)'=>[$q,$q]];
}
$where['is_delete']=0;
$data = model('stores')->pager($where);
foreach($data->data as $k=>$v){
$data->data[$k]->industry_lable=obj('app\block\common')->users_industry_lable($v->uid)->name;
$data->data[$k]->max_users=obj('app\block\common')->users_detail($v->uid)->max_users;
$data->data[$k]->fromto=obj('app\block\common')->fromto($v->uid);
}
return $data;
}
~~~
>[info]該條件為多個條件的查詢,此條件為or查詢。
那么下面介紹如何用and條件查詢,以上面的查詢為主:
~~~
$where['and_where'] = [
[ 'created >= ?' => [$start]],
[ 'created <= ?' => [$end]]
];
~~~
>[info]此條件為并且的關系,亦可理解為and查詢,如何只存在一個條件的情況下,可設置為$where['and_where'][]=['stores_id=?'=>$stores_users->stores_id];
還有一種情況為`in`條件查詢,同樣以上面范例為主:
~~~
$where['and_where'][]=['stores_id in ('.DB::in($f).')'=>$f];
~~~
>[info]$f在此不能為空,為空的情況下需特別定義為$con['stores_id']='';