SELECT
date_format(entrydate,'%Y-%m-%b') YearMonth,
gender,COUNT(1) GenderCount
FROM
patient_master
GROUP BY
date_format(entrydate,'%Y-%m-%b'),gender
;
這是你的樣本數據
mysql> CREATE TABLE patient_master
-> (
-> id int not null auto_increment,
-> gender varchar(10),
-> entrydate date,
-> primary key (id)
->);
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO patient_master (gender,entrydate) VALUES
-> ('Male' ,'2012-07-02'),
-> ('Female','2012-05-10'),
-> ('Male' ,'2012-05-25'),
-> ('Female','2012-07-09'),
-> ('Male' ,'2012-07-10'),
-> ('Female','2012-07-10'),
-> ('Male' ,'2012-07-10');
Query OK, 7 rows affected (0.06 sec)
Records: 7 Duplicates: 0 Warnings: 0
這裏是輸出
mysql> SELECT
-> date_format(entrydate,'%Y-%m-%b') YearMonth,
-> gender,COUNT(1) GenderCount
-> FROM
-> patient_master
-> GROUP BY
-> date_format(entrydate,'%Y-%m-%b'),gender
-> ;
+-------------+--------+-------------+
| YearMonth | gender | GenderCount |
+-------------+--------+-------------+
| 2012-05-May | Female | 1 |
| 2012-05-May | Male | 1 |
| 2012-07-Jul | Female | 2 |
| 2012-07-Jul | Male | 3 |
+-------------+--------+-------------+
4 rows in set (0.02 sec)
mysql>
傢伙就像從上面的例子,我想沒有在may..so結果月登記的性別將顯示,男性和女性在同樣的方式,它會顯示計數1 7月份的男性和女性的計數爲2 – 2012-07-10 20:29:57
傢伙我需要它爲multiseries線graph..so我想我需要的數據以這樣的方式,以便我可以繪製一年的所有月份。 – 2012-07-10 20:34:52
結果集應該是什麼形狀?多少行? 12? 24?多少列? 3? – 2012-07-10 20:36:50