Mysql 按护照类型选择被粘人数?

Mysql 按护照类型选择被粘人数?,mysql,sql,group-by,count,Mysql,Sql,Group By,Count,我创建了一个表: CREATE TABLE passeport ( numero_passeport INTEGER, IDadherent INTEGER, couleur VARCHAR(6), specialite VARCHAR(25), date_validation DATE, CONSTRAINT pkfh PRIMARY KEY (numero_passeport)); INSERT INTO passeport VALUES (1, 001, 'Blanc',

我创建了一个表:



CREATE TABLE passeport 
(
numero_passeport INTEGER, 
IDadherent INTEGER, 
couleur VARCHAR(6), 
specialite VARCHAR(25), 
date_validation DATE, 
CONSTRAINT pkfh PRIMARY KEY (numero_passeport));

INSERT INTO passeport VALUES (1, 001, 'Blanc', null, '1995/02/18');
INSERT INTO passeport VALUES (2, 002, 'Blanc', null, '2020/02/25');
INSERT INTO passeport VALUES (3, 003, 'Blanc', null, '2015/04/17');
INSERT INTO passeport VALUES (4, 004, 'Blanc', null, '2012/12/21');
INSERT INTO passeport VALUES (5, 005, 'Blanc', null, '1996/05/02');
INSERT INTO passeport VALUES (6, 006, 'Blanc', null, '2009/09/01');

INSERT INTO passeport VALUES (7, 001, 'Jaune', null, '1998/11/20');
INSERT INTO passeport VALUES (8, 003, 'Jaune', null, '2018/04/07');
INSERT INTO passeport VALUES (9, 004, 'Jaune', null, '2013/02/21');
INSERT INTO passeport VALUES (10, 005, 'Jaune', null, '2018/05/02');
INSERT INTO passeport VALUES (11, 006, 'Jaune', null, '2010/03/08');

INSERT INTO passeport VALUES (12, 001, 'Orange', null, '2000/03/23');
INSERT INTO passeport VALUES (13, 003, 'Orange', null, '2019/04/05');
INSERT INTO passeport VALUES (14, 004, 'Orange', null, '2014/08/06');
INSERT INTO passeport VALUES (15, 005, 'Orange', null, '2020/01/02');
INSERT INTO passeport VALUES (16, 006, 'Orange', null, '2017/01/01');

INSERT INTO passeport VALUES (17, 003, 'Vert', null, '2020/06/21');
INSERT INTO passeport VALUES (18, 004, 'Vert', null, '2016/01/15');
INSERT INTO passeport VALUES (19, 006, 'Vert', null, '2009/09/01');

INSERT INTO passeport VALUES (20, 004, 'Bleu', null, '2017/11/14');
INSERT INTO passeport VALUES (21, 006, 'Bleu', null, '2009/09/01');

INSERT INTO passeport VALUES (21, 004, 'Violet', null, '2018/10/14');
INSERT INTO passeport VALUES (22, 006, 'Violet', null, '2009/09/01');

INSERT INTO passeport VALUES (23, 004, 'Rouge', 'Performance', '2018/11/18');
INSERT INTO passeport VALUES (24, 006, 'Rouge', 'Grands espaces', '2011/05/02');

INSERT INTO passeport VALUES (25, 004, 'Rouge', 'Performance', '2019/12/17');
INSERT INTO passeport VALUES (26, 006, 'Rouge', 'Grands espaces', '2011/08/11');


INSERT INTO passeport VALUES (27, 006, 'Noir', 'Bloc', '2012/05/13');
INSERT INTO passeport VALUES (28, 006, 'Noir', 'voie', '2012/02/06');
INSERT INTO passeport VALUES (29, 006, 'Noir', 'grande voie', '2015/02/04');
INSERT INTO passeport VALUES (30, 006, 'Noir', 'compétition bloc', '2018/04/17');
INSERT INTO passeport VALUES (31, 006, 'Noir', 'compétition voie', '2019/12/29');
INSERT INTO passeport VALUES (32, 006, 'Noir','compétition grande voie', '2020/02/18');

我正试图做一个查询,以了解每种类型的护照有多少人持有

这里的问题是,passeport“noir”和“rouge”有不同的子类别

每次我尝试运行当前查询或其变体时,我的程序都会将持有护照不同子类别的同一个人计算为不同的个人:

查询:

SELECT COUNT(*), couleur FROM passeport GROUP BY couleur;

结果

+----------+---------+
| COUNT(*) | couleur |
+----------+---------+
|        6 | Blanc   |
|        5 | Jaune   |
|        5 | Orange  |
|        3 | Vert    |
|        2 | Bleu    |
|        4 | Rouge   |
|        6 | Noir    |
+----------+---------+
我希望得到一个人(IDadherent 006)的黑passeport


有人能帮忙吗?

如果我没弄错,你想按
couleur
计算distinct
IDadherent
s:

SELECT COUNT(DISTINCT IDadherent) no_adherent, couleur 
FROM passeport 
GROUP BY couleur;

您还可以在idadherant上使用组

模式(MySQL v8.0)


查询#1

SELECT COUNT(*), couleur FROM passeport GROUP BY couleur;

| COUNT(*) | couleur |
| -------- | ------- |
| 6        | Blanc   |
| 5        | Jaune   |
| 5        | Orange  |
| 3        | Vert    |
| 2        | Bleu    |
| 2        | Violet  |
| 4        | Rouge   |
| 6        | Noir    |

查询#2

SELECT COUNT(*),IDadherent, couleur FROM passeport GROUP BY couleur,IDadherent;

| COUNT(*) | IDadherent | couleur |
| -------- | ---------- | ------- |
| 1        | 1          | Blanc   |
| 1        | 2          | Blanc   |
| 1        | 3          | Blanc   |
| 1        | 4          | Blanc   |
| 1        | 5          | Blanc   |
| 1        | 6          | Blanc   |
| 1        | 1          | Jaune   |
| 1        | 3          | Jaune   |
| 1        | 4          | Jaune   |
| 1        | 5          | Jaune   |
| 1        | 6          | Jaune   |
| 1        | 1          | Orange  |
| 1        | 3          | Orange  |
| 1        | 4          | Orange  |
| 1        | 5          | Orange  |
| 1        | 6          | Orange  |
| 1        | 3          | Vert    |
| 1        | 4          | Vert    |
| 1        | 6          | Vert    |
| 1        | 4          | Bleu    |
| 1        | 6          | Bleu    |
| 1        | 4          | Violet  |
| 1        | 6          | Violet  |
| 2        | 4          | Rouge   |
| 2        | 6          | Rouge   |
| 6        | 6          | Noir    |

SELECT COUNT(*), couleur FROM passeport GROUP BY couleur;

| COUNT(*) | couleur |
| -------- | ------- |
| 6        | Blanc   |
| 5        | Jaune   |
| 5        | Orange  |
| 3        | Vert    |
| 2        | Bleu    |
| 2        | Violet  |
| 4        | Rouge   |
| 6        | Noir    |
SELECT COUNT(*),IDadherent, couleur FROM passeport GROUP BY couleur,IDadherent;

| COUNT(*) | IDadherent | couleur |
| -------- | ---------- | ------- |
| 1        | 1          | Blanc   |
| 1        | 2          | Blanc   |
| 1        | 3          | Blanc   |
| 1        | 4          | Blanc   |
| 1        | 5          | Blanc   |
| 1        | 6          | Blanc   |
| 1        | 1          | Jaune   |
| 1        | 3          | Jaune   |
| 1        | 4          | Jaune   |
| 1        | 5          | Jaune   |
| 1        | 6          | Jaune   |
| 1        | 1          | Orange  |
| 1        | 3          | Orange  |
| 1        | 4          | Orange  |
| 1        | 5          | Orange  |
| 1        | 6          | Orange  |
| 1        | 3          | Vert    |
| 1        | 4          | Vert    |
| 1        | 6          | Vert    |
| 1        | 4          | Bleu    |
| 1        | 6          | Bleu    |
| 1        | 4          | Violet  |
| 1        | 6          | Violet  |
| 2        | 4          | Rouge   |
| 2        | 6          | Rouge   |
| 6        | 6          | Noir    |