Mysql inq - 查询多个表的结果

时间:2013-10-28 15:17:19

标签: mysql count

我正在处理一个问题跟踪脚本并查看mysql语句我能够获得所有优先级问题的计数但却陷入了对状态的计数。

如何使用下面的mysql语句检索状态计数?

tbl_status
index    status
 1         open
 2         closed
 3         pending

tbl_priority
index      priority
1           low
2           medium
3             high


tbl_incident
incident  priority   status
adfadf        1          2
adfsdf        2          2
adfadf        1          1
adfadf        3          2
adfasdf       1          3

我能够将优先级分组(有效):

Low 3
Hedium 1
high 1

与状态相同的结果但是没有成功。也许从一个声明中要求太多。

open 1
closed 3
high 1
try
{   $stmt = $dbcon1->query("SELECT COUNT(tbl_incident.status),
               tbl_priority.priority, count(tbl_incident.priority), tbl_status.status
          FROM tbl_incident

     LEFT JOIN tbl_priority 
            ON tbl_priority.index = tbl_incident.priority

     LEFT JOIN tbl_status 
            ON tbl_status.index = tbl_incident.status       


      GROUP BY tbl_priority.pry_priority ");    
        $priorityCount = $stmt->fetchAll(PDO::FETCH_ASSOC);       

}

1 个答案:

答案 0 :(得分:0)

  

可能从单一声明中提出太多要求。

是的,尝试将每个问题的陈述分开。我认为这会更有效率。

状态:

select tbl_status.status, count(*)
from tbl_incident 
    inner join tbl_status on tbl_status.index = tbl_incident.status
group by tbl_status.status;

优先考虑:

select tbl_priority.priority, count(*)
from tbl_incident 
    inner join tbl_priority on tbl_priority.index = tbl_incident.priority
group by tbl_priority.priority;