为多个位置生成报告

1yjd4xko  于 2021-06-19  发布在  Mysql
关注(0)|答案(5)|浏览(299)

我试图在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\u 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 );

    }

现在我正在尝试将结果传递给视图,但是我收到了errors undefined变量:reports并尝试获取non object的属性。

vuktfyat

vuktfyat1#

以下代码按预期工作:
在控制器中

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>

感谢所有的贡献者。

c7rzv4ha

c7rzv4ha2#

如果我知道你想要什么,像这样的东西可以帮助你:

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

为了便于阅读,我把计数和平均值取了个别名

t5fffqht

t5fffqht3#

我想你需要这样的短信

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

tzxcd3kk4#

我想你需要having子句来表示聚合函数。像这样的方法可能有用:

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

uinbv5nw5#

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

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

相关问题