一,表的结构
SELECT * FROM t_unionpay_areacode t
SELECT * FROM t_unionpay_areacode t WHERE t.`name`LIKE "%林州%";
';
';
';
';
从上面的sql语句我们可以的出来一个规律就是县的paraent_code是市,而市的parent_code是省,此外level还有1,2,3级之分,分别对应着省市县
二,联动查询
2.1,省市左外连接查询
SELECT cy.NAME AS provice, cy.CODE AS proviceCode, ci.NAME AS city, ci.CODE AS cityCode FROM t_unionpay_areacode cy LEFT JOIN t_unionpay_areacode ci ON ci.parent_code = cy.CODE WHERE cy.NAME='河南省'
2.2,市县左外查询
SELECT cou1.NAME AS city, cou1.CODE AS cou1Code, cou2.NAME AS country, cou2.CODE AS cou2Code FROM t_unionpay_areacode cou1 LEFT JOIN t_unionpay_areacode cou2 ON cou2.parent_code = cou1.CODE AND cou1.NAME='安阳市'
2.3,通过市的code或者name相同两个让省市的左外连接在左外连接市县的左外连接
SELECT cy.NAME AS provice, cy.CODE AS proviceCode, ci.NAME AS city, ci.CODE AS cityCode, ct.country AS country, ct.cou2Code AS countryCode FROM t_unionpay_areacode cy LEFT JOIN t_unionpay_areacode ci ON ci.parent_code = cy.CODE LEFT JOIN (SELECT cou1.NAME AS city, cou1.CODE AS cou1Code, cou2.NAME AS country, cou2.CODE AS cou2Code FROM t_unionpay_areacode cou1 LEFT JOIN t_unionpay_areacode cou2 ON cou2.parent_code = cou1.CODE ) ct ON ci.NAME = ct.city ' ;