zoukankan      html  css  js  c++  java
  • 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();
    

      

  • 相关阅读:
    有关绑定没有数据显示的问题
    asp.net 打印控件之报表
    dockercompose安装,yml文件配置
    Docker日志文件切割以及大小配置
    查看Docker容器日志大小
    CSS控制 video 隐藏、显示进度条、播放按钮、全屏按钮
    根据url 下载文件
    自定义指令,按钮防连点
    iframe 内嵌页面以及传参
    C# 判断网站是否能访问或者断链
  • 原文地址:https://www.cnblogs.com/achengmu/p/6673566.html
Copyright © 2011-2022 走看看