Comme @Mark suggested, GROUP_CONCAT()
peut vous donner le résultat suivant:
SELECT id, subject, GROUP_CONCAT(mark SEPARATOR ' & ') marks
FROM results
GROUP BY id, subject;
+------+---------+---------+
| id | subject | marks |
+------+---------+---------+
| 1 | English | 50 & 60 |
| 1 | Maths | 32 |
| 1 | Science | 40 & 45 |
+------+---------+---------+
3 rows in set (0.00 sec)
A partir de ce cas de test:
CREATE TABLE results (id int, subject varchar(10), mark int);
INSERT INTO results VALUES (1, 'Maths', 32);
INSERT INTO results VALUES (1, 'Science', 40);
INSERT INTO results VALUES (1, 'Science', 45);
INSERT INTO results VALUES (1, 'English', 50);
INSERT INTO results VALUES (1, 'English', 60);
Cependant, une autre façon d'aborder le problème serait en utilisant comme sous requête pour chaque Sujet:
SELECT r.id,
(SELECT GROUP_CONCAT(r_eng.mark SEPARATOR ' & ')
FROM results r_eng
WHERE r_eng.subject = 'English' AND r_eng.id = r.id) English,
(SELECT GROUP_CONCAT(r_eng.mark SEPARATOR ' & ')
FROM results r_eng
WHERE r_eng.subject = 'Maths' AND r_eng.id = r.id) Maths,
(SELECT GROUP_CONCAT(r_eng.mark SEPARATOR ' & ')
FROM results r_eng
WHERE r_eng.subject = 'Science' AND r_eng.id = r.id) Science
FROM results r
GROUP BY r.id;
Qui donnera e résultat suivant:
+------+---------+-------+---------+
| id | English | Maths | Science |
+------+---------+-------+---------+
| 1 | 50 & 60 | 32 | 40 & 45 |
+------+---------+-------+---------+
1 row in set (0.01 sec)
MISE À JOUR:
Suite aux commentaires, il semble que vous devez prendre le champ year
en considération. Heureusement, la fonction GROUP_CONCAT()
prend une clause ORDER BY
que nous pouvons utiliser.Commençons par un nouveau cas de test avec le champ de l'année:
CREATE TABLE results (id int, subject varchar(10), mark int, year int);
INSERT INTO results VALUES (1, 'Maths', 32, 2008);
INSERT INTO results VALUES (1, 'Science', 40, 2009);
INSERT INTO results VALUES (1, 'Science', 45, 2008);
INSERT INTO results VALUES (1, 'English', 50, 2009);
INSERT INTO results VALUES (1, 'English', 60, 2008);
SELECT * FROM results;
+------+---------+------+------+
| id | subject | mark | year |
+------+---------+------+------+
| 1 | Maths | 32 | 2008 |
| 1 | Science | 40 | 2009 |
| 1 | Science | 45 | 2008 |
| 1 | English | 50 | 2009 |
| 1 | English | 60 | 2008 |
+------+---------+------+------+
5 rows in set (0.00 sec)
Ensuite, nous serions en mesure d'utiliser la fonction GROUP_CONCAT()
avec la clause ORDER BY
comme suit:
SELECT id,
subject,
GROUP_CONCAT(mark ORDER BY year SEPARATOR ' & ') marks,
GROUP_CONCAT(year ORDER BY year SEPARATOR ' & ') years
FROM results
GROUP BY id, subject;
+------+---------+---------+-------------+
| id | subject | marks | years |
+------+---------+---------+-------------+
| 1 | English | 60 & 50 | 2008 & 2009 |
| 1 | Maths | 32 | 2008 |
| 1 | Science | 45 & 40 | 2008 & 2009 |
+------+---------+---------+-------------+
3 rows in set (0.00 sec)
Enfin, pour GROUP BY
tout en un ligne horizontale, nous pouvons utiliser la technique de sous-requête que nous avons utilisé dans l'exemple précédent:
SELECT r.id,
(SELECT GROUP_CONCAT(r_eng.mark ORDER BY year SEPARATOR ' & ')
FROM results r_eng
WHERE r_eng.subject = 'English' AND r_eng.id = r.id) English,
(SELECT GROUP_CONCAT(r_eng.year ORDER BY year SEPARATOR ' & ')
FROM results r_eng
WHERE r_eng.subject = 'English' AND r_eng.id = r.id) Years_English,
(SELECT GROUP_CONCAT(r_eng.mark ORDER BY year SEPARATOR ' & ')
FROM results r_eng
WHERE r_eng.subject = 'Maths' AND r_eng.id = r.id) Maths,
(SELECT GROUP_CONCAT(r_eng.year ORDER BY year SEPARATOR ' & ')
FROM results r_eng
WHERE r_eng.subject = 'Maths' AND r_eng.id = r.id) Years_Maths,
(SELECT GROUP_CONCAT(r_eng.mark ORDER BY year SEPARATOR ' & ')
FROM results r_eng
WHERE r_eng.subject = 'Science' AND r_eng.id = r.id) Science,
(SELECT GROUP_CONCAT(r_eng.year ORDER BY year SEPARATOR ' & ')
FROM results r_eng
WHERE r_eng.subject = 'Science' AND r_eng.id = r.id) Years_Science
FROM results r
GROUP BY r.id;
qui retourne le fol mugissement:
+----+---------+---------------+-------+-------------+---------+---------------+
| id | English | Years_English | Maths | Years_Maths | Science | Years_Science |
+----+---------+---------------+-------+-------------+---------+---------------+
| 1 | 60 & 50 | 2008 & 2009 | 32 | 2008 | 45 & 40 | 2008 & 2009 |
+----+---------+---------------+-------+-------------+---------+---------------+
1 row in set (0.01 sec)
Si vous vouliez les marques et les années ordonnées par ordre décroissant, vous pouvez simplement ajouter le mot-clé DESC
après chaque ORDER BY year
.
En langage SQL pur ou PHP peut-il être utilisé? –