生成多个位置的报告

时间:2018-09-21 20:35:31

标签: php mysql sql codeigniter codeigniter-3

我正在尝试在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()结果返回空值之后

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

    }

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

enter image description here

enter image description here

5 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

我认为您需要这样的东西

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

答案 2 :(得分:0)

如果我了解您的需求,类似的方法可以提供帮助:

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 :(得分:0)

我认为您需要HAVING子句来实现汇总函数。 这样的事情可能会起作用:

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

答案 4 :(得分:0)

以下代码按预期工作:

在控制器中

select 
'item' = othertab..item,
'stockedFor' = tab..stocked_for
          + ', ' + tab..stockedFor2
          + ', '+ tab..stockedFor3

from tab
GROUP BY othertab..item,
         tab..stocked_for
          + ', ' + tab..stockedFor2
          + ', '+ tab..stockedFor3

order by case when stockedFor is null then 1 else 0 end, stockedFor

在视图中

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

    }

感谢所有贡献者。