내가 원하는 쿼리를 만드는 데 아무 문제가 있습니다.mysql join and count
DB는 위치와하는 것입니다,
t_location
관련된 다음과 같은 테이블이 있습니다 - 포함 사항 목록. 필드t_location_zipcode
및t_location_id_location
t_zipcodecity
- 조인 테이블 단지 t_zipcodecity_zipcode t_zipcodecity_id_cityt_city
-t_city_id_city
t_citystate
와 도시 목록 - 테이블, t_citystate_id_city, t_citystate_id_statet_state
가입 - t_state_id_state과 국가의 목록을
처음에이 쿼리를 사용하여 위치가있는 상태 목록을 얻으려고했습니다 :
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
ORDER BY `t_state_name_full` asc
잘 작동합니다.
이제 내가 싫어하는 것은 각 주마다 위치의 수를 얻는 것입니다. 이 쿼리에서이 작업을 수행 할 수 있는지 또는 다른 작업이 필요하면 도움이 필요한지 알 수 없습니다.
'City'와 'State'사이에 다 대다 관계가 있고 'City'와 'Location'이 다 대다 관계라는 점을 올바르게 이해하고 있습니까? –