0

次の(少し長い)クエリがあります:

 SELECT * 
        FROM client C
        JOIN uga U
        ON U.id_uga = C.id_uga
        JOIN appartenance A
        ON A.id_uga = U.id_uga
        JOIN serviceattribuee SA
        ON SA.id_client = C.id_client
        JOIN service S
        ON S.id_service = SA.id_service
        WHERE A.id_utilisateur = 28
        ORDER BY ville_client

次のようなものが返されます:

Nom : "Test" 
Adresse : "Test"
Services : "Service 1"

Nom : "Test" 
Adresse : "Test"
Services : "Service 2"


Nom : "Test 2"
Adresse : "Test 2"
Services : "Service 1"

Nom : "Test 2"
Adresse : "Test 2"
Services : "Service 2"

ご覧のとおり、クライアントごとに複数のサービスがあるため、結果が重複しています。ダブルスを避けるために、すべてのサービスを各クライアントの同じ行に連結する方法はありますか?

私はMySQL5.5を使用しています

ありがとう !

4

3 に答える 3

3
    SELECT c.Nom, c.Adresse, group_Concat(s.Nom) as services
    FROM client C
    JOIN uga U
    ON U.id_uga = C.id_uga
    JOIN appartenance A
    ON A.id_uga = U.id_uga
    JOIN serviceattribuee SA
    ON SA.id_client = C.id_client
    JOIN service S
    ON S.id_service = SA.id_service
    WHERE A.id_utilisateur = 28
    group by c.Nom, c.Adresse
于 2012-08-21T14:17:40.257 に答える
2

物事をグループ化し、group_concat を使用したい:

select nom, addresse, ville_client,
       group_concat(services separator ', ')
FROM client C JOIN
     uga U
     ON U.id_uga = C.id_uga JOIN
     appartenance A
     ON A.id_uga = U.id_uga JOIN
     serviceattribuee SA
     ON SA.id_client = C.id_client JOIN
     service S
     ON S.id_service = SA.id_service
group by nom, addresse, ville_client
WHERE A.id_utilisateur = 28
ORDER BY ville_client  
于 2012-08-21T14:20:13.000 に答える
2

I think what you are looking for is GROUP_CONCAT with a GROUP BY clause. In order to use it well, you will need to specifically enumerate the columns you want to concat (I've shown here a sample, you can adapt to your needs:

SELECT 
  C.*
  GROUP_CONCAT(S.Nom) as `services`
FROM client C
  JOIN uga U
  ON U.id_uga = C.id_uga
  JOIN appartenance A
  ON A.id_uga = U.id_uga
  JOIN serviceattribuee SA
  ON SA.id_client = C.id_client
  JOIN service S
  ON S.id_service = SA.id_service
WHERE A.id_utilisateur = 28
GROUP BY C.id
ORDER BY ville_client
于 2012-08-21T14:21:18.590 に答える