• 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)])
    

      

     andFilterWhere使用说明:

    当 WHERE 条件来自于用户的输入时,你通常需要忽略用户输入的空值。 例如,在一个可以通过用户名或者邮箱搜索的表单当中,用户名或者邮箱 输入框没有输入任何东西,这种情况下你想要忽略掉对应的搜索条件, 那么你就可以使用 yiidbQuery::filterWhere() 方法来实现这个目的:

    // $username 和 $email 来自于用户的输入$query->filterWhere([
        'username' => $username,
        'email' => $email,           
    ]);
    

      

    具体代码如下:

    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();
    

      

  • 相关阅读:
    pom变成红橙色
    拉链表
    Newsgroups数据集研究
    python 安装第三方包
    概率图(三)CRF条件随机场
    概率图(二)马尔科夫随机场MRF
    LDA主题模型
    TF-IDF算法
    mysql 自定义排序顺序
    [转]解决a different object with the same identifier value was already associated with the session错误
  • 原文地址:https://www.cnblogs.com/achengmu/p/6673566.html
Copyright © 2020-2023  润新知