2014-12-13 109 views
1

我想檢索一年中所有最大的疾病使用情況。我有一個名爲view_accepted_appoinments的表,它有列疾病和日期。我想這個查詢,但它不工作Mysqli查詢不起作用

select distinct doc_specilization as disease, 
DATE_FORMAT(date,'%Y') as year, 
count(Distinct doc_specilization) as count 
from view_accepted_appoinments 
group by year 
order by year desc; 

查詢返回這裏面給出錯誤的病名

+--------------+------+-------+ 
| disease  | YEAR | count | 
+--------------+------+-------+ 
| Primary Care | 2014 |  8 | 
| Primary Care | 2013 |  1 | 
+--------------+------+-------+ 
+0

在這個意義上不工作? ?你可以更清楚地瞭解你所得到的錯誤信息,或者顯示的數據與你所期望的輸出不同,並且你也可以向我們顯示錶格的列 – ManojGeek 2014-12-13 17:00:20

+0

這個查詢是否在執行時返回你預期的記錄集通過像MySQL Workbench或phpMyAdmin這樣的數據庫客戶端? – budhajeewa 2014-12-13 17:02:16

+0

你可以分享一些樣本數據和你試圖達到的結果嗎? – Mureinik 2014-12-13 17:04:59

回答

0
select distinct d , spec,c from(
     select date_format(date,"%Y") as d, 
     doctor, doc_specilization as spec, 
     count(doc_specilization) as c from 
     view_accepted_appoinments 
     group by doc_specilization, d)f 
     group by d; 
0

嘗試

SELECT doc_specialization AS disease, 
DATE_FORMAT(date, '%Y') AS year, 
COUNT(doc_specialization) AS count, 
FROM view_accepted_appoinments 
GROUP BY doc_specialization, year 
ORDER BY year DESC, doc_specialization ASC; 
+0

發生此錯誤 您的SQL語法錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在第4行的'FROM view_accepted_appoinments GROUP BY doc_specialization,year ORDER BY D'附近使用正確的語法。 – Zeeshan 2014-12-13 17:11:55

+0

@Zeeshan:您是否可以提供轉儲 - 最好帶有少量數據庫的實際數據?所以我可以導入它並測試? – budhajeewa 2014-12-13 17:14:36

+0

我爲此創建了一個邏輯表(視圖)。 – Zeeshan 2014-12-13 17:19:52