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"
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
@ user482957: Il vous manque le lien entre' tb' et 't', dans la clause' where' – Andomar