【问题标题】:Grouping multiple Mysql statements to fetch count of multiple statuses [duplicate]分组多个Mysql语句以获取多个状态的计数[重复]
【发布时间】:2021-10-06 06:22:40
【问题描述】:

目前我正在使用 Mysql 和 CodeIgniter 在特定时间范围内从我的数据库中获取我的条目。每个条目在数据库中的状态为 D、N、Y。现在要显示这些数据,我对要分组为 1 个语句的每个状态都有不同的语句。

模型类:

public function get_records_draft($st_date,$end_date){
        $this->db->select('*');
        $this->db->from('crm_listings');
        $this->db->where('cast(added_date as date) BETWEEN "' . $st_date . '" AND "' . $end_date . '" AND status= "D"');

         return $this->db->get();
     }

     public function get_records_unpublish($st_date,$end_date){
        $this->db->select('*');
        $this->db->from('crm_listings');
        $this->db->where('cast(added_date as date) BETWEEN "' . $st_date . '" AND "' . $end_date . '" AND status= "N"');

        return $this->db->get();
     }

     public function get_records_publish($st_date,$end_date){
        $this->db->select('*');
        $this->db->from('crm_listings');
        $this->db->where('cast(added_date as date) BETWEEN "' . $st_date . '" AND "' . $end_date . '" AND status= "Y"');

        return $this->db->get();
     }

还有我的控制器类,我在其中获取这些数据并以表格的形式显示它:

$data = $this->user_model->get_records_draft($startDate,$endDate);
$data1 = $this->user_model->get_records_unpublish($startDate,$endDate);
$data2 = $this->user_model->get_records_publish($startDate,$endDate);

$output .= '
              <div class="table-responsive">
                 <table class="table table-bordered table-striped">
                    <tr>
                    <th>Draft</th>
                    <th>Unpublish</th>
                    <th>Publish</th>
                    </tr>
              ';
           if($data->num_rows() > 0)
           {
            $output .= '
               <tr>
               <td>'.$data->num_rows().'</td>
               <td>'.$data1->num_rows().'</td>
               <td>'.$data2->num_rows().'</td>
               </tr>
            ';
            
           }
           else
           {
              $output .= '<tr>
                 <td colspan="5">No Data Found</td>
                 </tr>';
           }
           $output .= '</table>';
           echo $output;

这是我的视图类中的 AJAX 请求:

$('#alertbox').click(function(){
            var startDate = $('#startDate').val();
            var endDate = $('#endDate').val();
            // var status = $('#status').val();
            if(startDate != '' && endDate != '' ){
            $.ajax({
                url:"<?php echo base_url(); ?>testcontroller/fetch_status",
                method:"POST",
                data:{startDate:startDate, endDate:endDate},
                success:function(data){
                    $('#result').html(data)
                }
            })
            }else{
                alert("Please enter a date");
            }
        })

我的问题是,无论如何将所有这些分组为 1 个方法,该方法将它们全部分类到我的控制器类中的特定标题中。

我曾尝试在 phpmyadmin 中使用以下查询,它给了我正确的输出,但我不知道如何在我的模型和控制器类中执行相同的操作:

SELECT COUNT(status) FROM `crm_listings` WHERE added_date BETWEEN '2021-09-23' AND '2021-09-29' GROUP BY status

【问题讨论】:

  • 我遇到过类似的问题,我正在尝试的最佳方法是使用db-&gt;query 并创建查询以获取包含所有总和数据的一行结果
  • @adam 我希望系统地显示它,所以我在控制器类中创建了&lt;table&gt; 标签。但我不认为我可以通过使用查询方法来显示我想要的方式

标签: php mysql codeigniter


【解决方案1】:

使用CASE 语句代替多个查询

型号:

public function summary($st_date,$end_date){
      $this->db->select("
         SUM(CASE WHEN status = 'D' THEN 1 END) AS draft,
         SUM(CASE WHEN status = 'N' THEN 1 END) AS unpublish,
         SUM(CASE WHEN status = 'Y' THEN 1 END) AS publish"
      );
      $this->db->where('added_date >=', $st_date);
      $this->db->where('added_date <=', $end_date);
      return $this->db->get('crm_listings');
 }

查看:

不要在控制器中创建 HTML,因为这在 MVC 中是一种不好的做法。在视图文件中使用foreach 循环来显示值 阅读更多来自CI Views

<div class="table-responsive">
    <table class="table table-bordered table-striped">
        <tr>
           <th>Draft</th>
           <th>Unpublish</th>
           <th>Publish</th>
         </tr>
    <?php      
       if(isset($data) && count($data) > 0){
          foreach($data as $row ){ ?>
           <tr>
           <td><?= $row->draft ?></td>
           <td><?= $row->unpublish ?></td>
           <td><?= $row->publish ?></td>
           </tr>
       <?php } //Foreach end here

          } else { ?>
          <tr>
             <td colspan="5">No Data Found</td>
          </tr>
       <?php } ?>
      </table>

阅读更多关于MySQL Case Statement

【讨论】:

  • 此语句在 phpmyadmin 中有效,但是当我在我的页面上尝试它时,它没有显示任何内容并在控制台中显示此内容加载资源失败:服务器响应状态为 500(内部服务器错误)
  • @JJM50 请分享完整的错误信息
  • 问题是我没有收到任何错误消息,只是我的网页中没有弹出任何内容,我的控制台中只收到这 1 条消息
  • @JJM50 没有错误详细信息有什么可以帮助您的吗?顺便说一句,你在使用 AJAX 请求吗?
  • 是的,它是一个 AJAX 请求。我还尝试在我的控制器类之上添加ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);,但它没有显示任何错误。我在问题中添加了 AJAX 请求以供参考
【解决方案2】:

试试这个

Your models
    public function summary($st_date,$end_date){
            $this->db->select("
            SUM(CASE WHEN status = 'D' THEN 1 END) AS Draft,
            SUM(CASE WHEN status = 'N'  THEN 1 END) AS Unpublish,
            SUM(CASE WHEN status = 'Y' THEN 1 END) AS Publish,
            "
            );
            $this->db->from('crm_listings');
            $this->db->where('cast(added_date as date) BETWEEN "' . $st_date . '" AND "' . $end_date');
    
            return $this->db->get();
         }

控制器

....
$data = $this->user_model->summary($startDate,$endDate)->result();

echo $data->Draft;
echo $data->Unpublish;
echo $data->Publish;

【讨论】:

  • 试过了,没有显示输出。它在控制台中显示这个加载资源失败:服务器响应状态为 500(内部服务器错误)
  • @JJM50 检查你的数据库连接
  • 开发环境设置了吗?
  • 丹麦阿里,是的,没错,我忘记写了,,,,再试一次@JJM50
猜你喜欢
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 2017-09-06
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
相关资源
最近更新 更多