【问题标题】:Retrieving number of rows with a particular value after filtering query through date picker [duplicate]通过日期选择器过滤查询后检索具有特定值的行数[重复]
【发布时间】:2021-11-25 17:43:48
【问题描述】:

目前我正在使用 CodeIgniter 在特定时间范围内检索我的数据。所有这些条目都有一个状态。我想对所有具有相同状态的条目进行分组,并将其显示在各自的标题中。目前这是我的模型类,其中我有以下条目来返回特定日期范围内的所有条目:

public function get_records($st_date,$end_date){
        $this->db->select('*');
        $this->db->from('crm_listings');
        $this->db->where('(added_date BETWEEN "' . $st_date . '" AND "' . $end_date . '")');
        
        echo $this->db->count_all_results();
     }

还有我的控制器类:

function fetch_status(){
            $output ='';
            $startDate = '';
            $endDate = '';
            $this->load->model('crm/user_model');
  
            if($this->input->post('startDate')){
              $startDate = $this->input->post('startDate');
            }
            if($this->input->post('endDate')){
               $endDate = $this->input->post('endDate');
             }
            $data = $this->user_model->fetch_date($startDate,$endDate);
            
            $output .= '
              <div class="table-responsive">
                 <table class="table table-bordered table-striped">
                    <tr>
                    <th>Draft</th>
                    <th>Unpublish</th>
                    <th>Publish</th>
                    <th>Action</th>
                    <th>Unlisted</th>
                    <th>Sold</th>
                    <th>Let</th>
                    </tr>
              ';
           if($data->num_rows() > 0)
           {
            foreach($data->result() as $row)
            {
            $output .= '
               <tr>
                  //Dont know what to put in here
               </tr>
            ';
            }
           }
           else
           {
              $output .= '<tr>
                 <td colspan="7">No Data Found</td>
                 </tr>';
           }
           $output .= '</table>';
           echo $output;
           }

我已经尝试在我的 phpmyadmin 中使用以下命令,它给了我想要的输出,但我不知道如何在 codeigniter 中进行查询。

SELECT sum(case when status = 'D' then 1 else 0 end) AS Draft,
    sum(case when status = 'L' then 1 else 0 end) AS Let,
    id FROM `crm_listings`  
WHERE added_date BETWEEN '2021-09-24' AND '2021-09-28' ORDER BY `added_date` DESC

这里的日期必须替换为 $startDate 和 $endDate。

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:
$sql = "SELECT
            sum(case when status = 'D' then 1 else 0 end) AS Draft,
            sum(case when status = 'L' then 1 else 0 end) AS Let,
            id
        FROM `crm_listings`  
        WHERE added_date BETWEEN '" . $st_date . "' AND '".$end_date."'
        ORDER BY `added_date` DESC";
$response = count($this->db->query($sql));
echo $response;

echo $response->num_rows();

【讨论】:

    猜你喜欢
    • 2011-09-01
    • 1970-01-01
    • 2023-01-27
    • 2017-07-11
    • 2021-02-13
    • 2016-01-13
    • 2013-01-14
    • 2011-11-07
    • 2020-05-31
    相关资源
    最近更新 更多