首页 > 代码库 > yii2:多条件多where条件下碰到between时,between语句如何处理呢?
yii2:多条件多where条件下碰到between时,between语句如何处理呢?
yii2:多条件多where条件下碰到between时,between语句如何处理呢?
我有一张表:
id,name,telphone,ticket_no,status,create_time等字段,
在出具多条件查询时(当不涉及到时间范围或其他范围),可以用如下语句:
if (!empty($params[‘id‘])) { $where_condition[‘oid‘] = $params[‘id‘]; } if (!empty($params[‘post_name‘])) { $where_condition[‘post_name‘] = $params[‘post_name‘]; } if (!empty($params[‘telephone‘])) { $where_condition[‘telephone‘] = $params[‘telephone‘]; } if (!empty($params[‘ticket_no‘])) { $where_condition[‘ticket_no‘] = $params[‘ticket_no‘]; } if ($params[‘status‘] != -1) { $where_condition[‘status‘] = $params[‘status‘]; } $where_condition[‘delete_flg‘] = 0; $count = static::find() ->where($where_condition) ->andFilterWhere([‘between‘,‘CREATE_TIME‘,$start_date, $end_date]) ->count();
当涉及到create_time时间范围查询时,那么问题来了,between怎么加进去?$where_condition[‘CREATE_TIME‘]=‘between 时间1 and 时间2‘ 这样是不行的,花了一点时间查询了下,yii2有这样的方法:andFilterWhere,使用方法如下:
->andFilterWhere([‘like1‘, ‘name‘, ‘%a%‘]) #当参数1,参数2为空时,between方法会自动过滤掉,也就是此条件会被删除不执行 ->andFilterWhere([‘between‘, ‘created_at‘, 0(参数1), 1433088000(参数2)])
具体代码如下:
if (!empty($params[‘id‘])) { $where_condition[‘oid‘] = $params[‘id‘]; } if (!empty($params[‘post_name‘])) { $where_condition[‘post_name‘] = $params[‘post_name‘]; } if (!empty($params[‘telephone‘])) { $where_condition[‘telephone‘] = $params[‘telephone‘]; } if (!empty($params[‘ticket_no‘])) { $where_condition[‘ticket_no‘] = $params[‘ticket_no‘]; } if ($params[‘status‘] != -1) { $where_condition[‘status‘] = $params[‘status‘]; } $start_date = $end_date = ‘‘; if($params[‘isSearch‘] == 1) { if (!empty($params[‘start_date‘]) && !empty($params[‘end_date‘])) { $start_date = strtotime($params[‘start_date‘]); $end_date = strtotime($params[‘end_date‘]) + 86400 - 1; } } $where_condition[‘delete_flg‘] = 0; $count = static::find() ->where($where_condition) ->andFilterWhere([‘between‘,‘CREATE_TIME‘,$start_date, $end_date]) ->count();
yii2:多条件多where条件下碰到between时,between语句如何处理呢?
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。