【问题标题】:Generate a report for multiple locations为多个位置生成报告
【发布时间】:2018-09-21 20:35:31
【问题描述】:

我正在尝试在 CodeIgniter 中实现以下结果

SELECT location, COUNT(location), AVG(review) FROM progrodb.tickets WHERE datesubmitted BETWEEN '2018-9-1' AND '2018-9-30' AND location = 'location'

输出需要是;

Location|Total Tickets|Avg Review<br>
location|3            |4.5

此表应包含每个位置的结果。 SQL 语句按原样提供单个位置的结果,现在我需要为总共 22 个位置执行此操作。

我尝试了以下尝试,但是在 var_dump() 结果返回 null 之后

public function generatereport(){
        // Set Page Title
        $this->data['page_title'] = 'Generate Report';

        $rules = $this->support_m->rules_report;
        $this->form_validation->set_rules($rules);

        $startdate = $this->input->post('startdate');
        $enddate = $this->input->post('enddate');
        define('locations', array('Shoppers Fair Blue Diamond', 'Shoppers Fair Burke Road', 'Shoppers Fair Brunswick', 'Shoppers Fair Duhaney Park', 'Shoppers Fair Greater Portmore', 'Shoppers Fair View', 'Shoppers Fair Junction', 'Shoppers Fair Liguanea', 'Shoppers Fair Manchester'));

         if ($this->form_validation->run() == TRUE){
            $results = $this->db->select('location, count(location) as location_count, AVG(review) as review_avg')
            ->where('datesubmitted BETWEEN "'.$startdate.'" AND "'.$enddate.'"')
            ->group_by('location') 
            ->get('tickets')->result();
            var_dump($results);

         }

        // Load view    
        $this->data['subview'] = 'admin/tickets/report';
        $this->load->view( 'admin/body', $this->data );

    }

现在得到了以下转储,我试图将结果传递给视图,但收到错误未定义变量:报告并尝试获取非对象的属性。

【问题讨论】:

  • 它是正确的,因为在你的 where 条件下有 "where location='locaition'" 请注意,你应该在使用聚合函数时使用 groupy
  • 是的,只需删除 AND location='location' 或者如果您想限制结果数量,请使用 LIMIT 关键字

标签: php mysql sql codeigniter codeigniter-3


【解决方案1】:

您可以使用以下方法为多个位置生成报告:

   $results = $this->db->select('count(location), AVG(review)')
   ->where('datesubmitted BETWEEN "2018-9-1" AND "2018-9-30"')
   ->group_by('location') 
   ->get('tickets')->result();

【讨论】:

  • 取决于位置和您可能需要使用的位置having()
  • 然后把它放在你的答案中,而不是放在可能永远不会被阅读的评论中
  • 我做了以下但我试图显示每次旋转的结果: if ($this->form_validation->run() == TRUE){ foreach (locations as $location => $value){ $results = $this->db->select('location, count(location), AVG(review)') ->where('datesubmitted BETWEEN "'.$startdate.'" AND "'.$ enddate.'" AND location="'.$value.'"') ->get('tickets')->result(); } } 现在我正在尝试显示结果
【解决方案2】:

如果我明白你想要什么,这样的事情会有所帮助:

SELECT location, COUNT(location) as location_count, AVG(review) as review_avg
FROM progrodb.tickets
WHERE datesubmitted BETWEEN '2018-09-01' AND '2018-09-30'
GROUP BY location

为了便于阅读,我为计数和平均值设置了别名

【讨论】:

    【解决方案3】:

    我觉得你需要这样的东西

    SELECT location, COUNT(location), AVG(review) 
    FROM progrodb.tickets
    WHERE datesubmitted BETWEEN '2018-9-1' AND '2018-9-30'
    GROUP BY location = 'location'
    

    【讨论】:

      【解决方案4】:

      我认为聚合函数需要 HAVING 子句。 这样的事情可能会奏效:

      SELECT
          location,
          COUNT( location ),
          AVG( review ) 
      FROM
          progrodb.tickets 
      WHERE
          datesubmitted BETWEEN '2018-9-1' 
          AND '2018-9-30' 
      HAVING
          location = 'location'
      

      【讨论】:

        【解决方案5】:

        以下代码按预期工作:

        在控制器中

        public function generatereport(){
                // Set Page Title
                $this->data['page_title'] = 'Generate Report';
                $this->data['reports'] = null;
        
                $rules = $this->support_m->rules_report;
                $this->form_validation->set_rules($rules);
        
                $startdate = $this->input->post('startdate');
                $enddate = $this->input->post('enddate');
        
                if ($this->form_validation->run() == TRUE){
                    $results = $this->db->select('location, count(location) as locationcount, AVG(review) as reviewavg')
                    ->where('datesubmitted BETWEEN "'.$startdate.'" AND "'.$enddate.'"')
                    ->group_by('location') 
                    ->get('tickets')->result();
                    $this->data['reports'] = $results;
                }
        
                // Load view    
                $this->data['subview'] = 'admin/tickets/report';
                $this->load->view( 'admin/body', $this->data );
        
            }
        

        在视图中

        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Location</th>
                                    <th>Total Tickets</th>
                                    <th>Avg Review</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                    if(count($reports)): foreach($reports as $ticketreport): 
                                ?>
                                <tr>
                                    <td><?php echo $ticketreport->location; ?></td>
                                    <td><?php echo $ticketreport->locationcount; ?></td>
                                    <td><?php echo $ticketreport->reviewavg; ?></td>
                                </tr>
                                <?php endforeach; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="4">We could not find any records.</td>
                                    </tr>
                                    <?php endif; ?> 
                            </tbody>
                        </table>
        

        感谢所有做出贡献的人。

        【讨论】:

          猜你喜欢
          • 2021-09-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-02
          相关资源
          最近更新 更多