Bonjour,
J'ai une table de ce type:
Prof Groupe Etudiant
Prof1 Gr1 Etudiant1
Prof1 Gr1 Etudiant2
Prof1 Gr2 Etudiant3
Prof2 Gr1 Etudiant1
Prof2 Gr1 Etudiant2
En une seule requête je voudrais avoir le résultat suivant (Je concatène les noms des étudiants pour un même prof et un même groupe):
Prof1 Gr1 Etudiant1, Etudiant2
Prof1 Gr2 Etudiant3
Prof2 Gr1 Etudiant1, Etudiant2
J'ai pensé à une requête de ce type:
Mais la requête ne fonctionne pas ...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 select Prof, Groupe, IF(@ListeEtudiant='', @ListeEtudiant:=test1.Etudiant, @ListeEtudiant:=CONCAT(@ListeEtudiant, ", ",test1.Etudiant) ) as Liste from Test1, (select @ListeEtudiant='') r group by Prof, Groupe
Elle retourne ceci
"Prof1" "Gr1" "Etudiant1"
"Prof1" "Gr2" "Etudiant1, Etudiant2, Etudiant3"
"Prof2" "Gr1" "Etudiant1, Etudiant2, Etudiant3, Etudiant1"
J'ai donc décide de réinitialiser ma liste à chaque fois que je change de regroupement en faisant ceci :
Mais le résultat n'est toujours pas correct
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 select Prof, Groupe, IF(INSTR(@ListeEtudiant, CONCAT(Prof, Groupe))= 0, @ListeEtudiant:=CONCAT("[", Prof, Groupe, "]", test1.Etudiant), @ListeEtudiant:=CONCAT(@ListeEtudiant, ", ",test1.Etudiant) ) as Liste from Test1, (select @ListeEtudiant:='') r group by Prof, Groupe;
"Prof1" "Gr1" "[Prof1Gr1]Etudiant1"
"Prof1" "Gr2" "[Prof1Gr2]Etudiant3"
"Prof2" "Gr1" "[Prof2Gr1]Etudiant1"
Quelqu'un a une idée (J'utilise MySQL 5.0.27) ?
Merci d'avance
Partager