2010-10-21 5 views
1

J'essaie de montrer les arrondissements et les codes postaux d'une ville en particulier.mysql multiple-subquery group_concat requête

Ma base de données est assez bien structurée, avec une table telle que la ville, le code postal et l'arrondissement. Il y a aussi des tables pour chacune des relations town_postcode & town_borough.

Idéalement je veux les données renvoyées comme:

"Abbey Wood", "SE2", "Bexley, Greenwich" "Barbican", "EC1, EC2", "City of London"

J'ai essayé quelques approches différentes et je suis proche mais pas encore là.

Toute aide serait appréciée ... :) Jusqu'à présent, j'ai essayé

SELECT DISTINCT t.town, 
GROUP_CONCAT(DISTINCT p.postcode SEPARATOR ', ') AS 'postcode', 
GROUP_CONCAT(DISTINCT b.borough SEPARATOR ', ') AS 'borough' 
FROM coverage_towns AS t, 
coverage_boroughs AS b, 
coverage_postcodes AS p, 
coverage_towns_boroughs AS tb, 
coverage_towns_postcodes AS tp 
WHERE t.id = tp.town_id 
AND p.id = tp.postcode_id 
AND b.id = tb.borough_id 
GROUP BY t.town 
ORDER BY t.town ASC 

qui retourne

"Abbey Wood", "SE2", "Southwark, Hammersmith and Fulham, Tower Hamlets, Wandsworth, Enfield, Newham, LOTS MORE HERE" 
"Barbican", "EC1, EC2", "Brent, Greenwich, Kensington and Chelsea, Westminster, Camden, LOTS MORE HERE" 

J'ai aussi essayé

SELECT DISTINCT t.town, (

SELECT SQL_CACHE DISTINCT GROUP_CONCAT(p1.postcode 
SEPARATOR ', ') 
FROM coverage_postcodes AS p1 
WHERE p1.id = tp.postcode_id 
) AS 'postcode', (

SELECT SQL_CACHE DISTINCT GROUP_CONCAT(b1.borough 
SEPARATOR ', ') 
FROM coverage_boroughs AS b1 
WHERE b1.id = tb.borough_id 
) AS 'borough' 
FROM coverage_towns AS t, coverage_boroughs AS b, coverage_postcodes AS p, coverage_towns_boroughs AS tb, coverage_towns_postcodes AS tp 
WHERE t.id = tp.town_id 
AND p.id = tp.postcode_id 
AND b.id = tb.borough_id 
GROUP BY t.town 
ORDER BY t.town ASC 

qui retourne

"Abbey Wood", "SE2", "Greenwich" 
"Acton", "W3", "Greenwich" 
"Aldersbrook", "E12", "Greenwich" 

Répondre

1

Première requête semble bon, il suffit d'ajouter distinct à l'intérieur du group_concat, comme:

SELECT t.town 
,  GROUP_CONCAT(DISTINCT p.postcode SEPARATOR ', ') AS 'postcode' 
,  GROUP_CONCAT(DISTINCT b.borough SEPARATOR ', ') AS 'borough' 
<more code here> 
GROUP BY 
     t.town 
+0

C'est certainement une amélioration sur la colonne de the2nd, mais maintenant je "Abbey Wood", "SE2", "Southwark, Hammersmith et Fulham, Tower Hamlets, Wandsworth, Enfield, Newham, BEAUCOUP PLUS ICI" " Barbican "," EC1, EC2 "," Brent, Greenwich, Kensington et Chelsea, Westminster, Camden, BEAUCOUP PLUS ICI " où je cherche: " Abbey Wood "," SE2 "," Bexley, Greenwich " " Barbican "," EC1, EC2 "," Ville de Londres " – user482957

+1

@ user482957: Il vous manque le lien entre' tb' et 't', dans la clause' where' – Andomar

1

SOLUTION

je suis revenu à la question après un bon café et la réponse elle-même présenté.

SELECT DISTINCT t.town, 
GROUP_CONCAT(DISTINCT p.postcode SEPARATOR ', ') AS 'postcode', 
GROUP_CONCAT(DISTINCT b.borough SEPARATOR ', ') AS 'borough' 
FROM towns AS t, boroughs AS b, postcodes AS p, towns_boroughs AS tb, towns_postcodes AS tp 
WHERE (t.id = tp.town_id AND t.id = tb.town_id) 
AND (p.id = tp.postcode_id AND b.id = tb.borough_id) 
GROUP BY t.town 
ORDER BY t.town ASC