2017-09-05 69 views
1

我打算畫一個條形圖。 x軸包含標籤。標籤是從一個mysql請求生成的。標籤也太多,不適合頁面圖形。我打算將一些熟悉的標籤合併成一個。我相信存在設置是好的,但我想編輯我的MySQL查詢來適應這個問題。如何將多個結果連接成mysql中的一些通用結果?

清除問題的示例。

這是我目前MySQL查詢

$query = "SELECT DISTINCT HITYPE, 
      SUM(IF(STATUS = 'OPEN',1,0)) AS OPEN, 
      SUM(IF(STATUS = 'CLOSED',1,0)) AS CLOSED, 
      COUNT(STATUS) AS 'TOTAL' 
      FROM hazardreport WHERE HITYPE IS NOT NULL AND RISKLEVEL='$risklevel' AND DATE(DREPORTED) BETWEEN '$dateone' AND 
      '$datetwo' 
      GROUP BY HITYPE 
      ORDER BY TOTAL DESC"; 

所生成的數據是

item1, 1, 0, 1 
item1.1 , 3 , 2 , 5 
item2 , 1 , 1 , 2 
item2.2, 5, 1 , 6 

我想編輯查詢,以便所生成的數據可以組合到:

item 1, 4, 2 , 6 
item 2, 6, 2, 8 

我希望這是有道理的。

回答

0

嘗試按hitype字符串值的第一部分對結果進行分組。

SELECT HITYPE, 
SUM(IF(STATUS = 'OPEN',1,0)) AS OPEN, 
SUM(IF(STATUS = 'CLOSED',1,0)) AS CLOSED, 
COUNT(STATUS) AS TOTAL 
from (
    select SUBSTRING_INDEX(HITYPE, '.', 1) as HITYPE, status 
    FROM hazardreport WHERE HITYPE IS NOT NULL AND RISKLEVEL='$risklevel' 
    AND DATE(DREPORTED) BETWEEN '$dateone' AND '$datetwo' 
) as results 
GROUP BY HITYPE 
ORDER BY TOTAL DESC 
相關問題