2

説明されているように、性別と日付の列があるテーブルを持っている人がいます。今、私は特定の月に登録された両方の男性と女性を数えたいので、どうすればそれを照会できますか。

mysql> select id,gender,entrydate from patient_master;

+----+--------+------------+
| id | gender | entrydate  |
+----+--------+------------+
|  1 | Male   | 2012-07-02 |
|  2 | Female | 2012-05-10 |
|  3 | Male   | 2012-05-25 |
|  4 | Female | 2012-07-09 |
|  5 | Male   | 2012-07-10 |
|  6 | Female | 2012-07-10 |
|  7 | Male   | 2012-07-10 |
+----+--------+------------+
4

4 に答える 4

2
select gender, count(id) as`count` 
from patient_master
where month(entrydate) = 1 and year(entrydate) = 2012
group by gender

1=1月、2=2月..。

于 2012-07-10T20:24:14.113 に答える
0

すべての月の結果を取得するには:

SELECT YEAR(entrydate) AS yr, MONTH(entrydate) AS mnth, gender, COUNT(*) AS cnt
FROM patient_master
GROUP BY YEAR(entrydate), MONTH(entrydate), gender

1年のすべての月にそれが必要な場合:

SELECT MONTH(entrydate) AS mnth, gender, COUNT(*) AS cnt
FROM patient_master
WHERE YEAR(entrydate) = 2012
GROUP BY MONTH(entrydate), gender
于 2012-07-10T20:24:23.893 に答える
0
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>
于 2012-07-10T20:30:46.433 に答える
0

@juergen_dは正しいです。

彼のクエリはこれを返します:

+--------+--------+
| gender | count  |
+--------+--------+
| Male   |  1     |
| Female |  1     |
+--------+--------+
于 2012-07-10T20:31:10.833 に答える