mySQL查询:通过重复注册PHP计数和排序

时间:2018-06-12 19:56:23

标签: php mysql sql symfony

我需要使用php为我的票证系统生成统计信息。 在表tickets中,每个故障单都分配给技术人员,请求支持的公司存储在project_id

所以,我想知道哪些公司有更多问题。 我尝试了thisthis,这确实帮助我统计了我所拥有的90家公司中的22家公司,开了门票。

由于我需要展示票数最多的前5家公司,我写了这个函数:

public static function getAllProjectsWithProblems(){
    $sql = "select project_id, count(*) as c from ".self::$tablename." group by project_id having count(project_id)>=1 order by c desc limit 5";
    $query = Executor::doit($sql);
    return Model::many($query[0],new TicketData());
}

现在,我需要知道他们拥有的门票数量。

这是DB的一个例子:

id --------------     description    ------- project_id 
1  -------------- This is an example -------     1
2  -------------- This is an example -------     1
3  -------------- This is an example -------     1
4  -------------- This is an example -------     1
5  -------------- This is an example -------     2
6  -------------- This is an example -------     2
7  -------------- This is an example -------     2
8  -------------- This is an example -------     3
9  -------------- This is an example -------     4
10 -------------- This is an example -------     4
11 -------------- This is an example -------     4
12 -------------- This is an example -------     4
13 -------------- This is an example -------     4
14 -------------- This is an example -------     4
15 -------------- This is an example -------     5

期望的结果:

Companies with issues: (5)
Company 4 = 6 issues
Company 1 = 4 issues
Company 2 = 3 issues
Company 3 = 1 issue
Company 5 = 1 issue

我得到的结果是:

Companies with issues: (5)
Company 4
Company 1
Company 2
Company 3
Company 5

这是HTML:

       <div class="card" style="position: relative; left: 0px; top: 0px;">
           <div class="card-header ui-sortable-handle" style="cursor: move;">
             <h3 class="card-title">
               <i class="ion ion-clipboard mr-1"></i>
               Top 5 companies with issues
             </h3>
           </div>
           <div class="card-body">
               <ul class="todo-list ui-sortable">
                 <?php
                 $users = array();{
                     $users = TicketData::getAllProjectsWithProblems();             
                 }
                 if (count($users) > 0) {
                     // count if there are users
                 ?>
                    <?php
                        foreach ($users as $user) {
                         $project = $user->getProject();
                    ?>
                    <li>
                    <span class="text"> <?php echo$project->id; ?>: //insert number of tickets here// </span>
                 </li>
                    <?php } ?>
                 <?php
                    } else {
                        echo "<p class='alert alert-danger'>You don't have open tickets.</p>";
                    }
                 ?>
             </ul>
           </div>

0 个答案:

没有答案