假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击
一,表一的数据
1.1,表的基础数据
注意这张表中市县的parentid都是河南省,所以区别不出来2.3级的关系,而code又没有具体的关联
1 SELECT * FROM t_zone_info t WHERE t.`parentId`='16' OR t.`id`='16';
1.2,表一左外连接
1 SELECT 2 prov.NAME AS province, 3 prov.CODE AS provinceCode, 4 cti.NAME AS city, 5 cti.CODE AS cityCode 6 FROM 7 t_zone_info prov 8 LEFT JOIN t_zone_info cti 9 ON prov.id = cti.parentId 10 WHERE prov.CODE = '410000' 11 ORDER BY province DESC,12 cityCode
2,表二的数据结构
2.1,表的基础数据,这个表有明显的1,2,3级关系的,在parent_code中有明显的标记的,不想上一张表
1 SELECT * FROM t_unionpay_areacode t WHERE t.`name`LIKE "%林州%";
1 SELECT * FROM t_unionpay_areacode t WHERE t.`parent_code`= '4960';
1 SELECT * FROM t_unionpay_areacode t WHERE t.code= '4960';
1 SELECT * FROM t_unionpay_areacode t WHERE t.`parent_code`='4900';
2.2,表二的左外连接(只查市县的左外连接)
1 SELECT 2 cou1.NAME AS city,3 cou2.NAME AS country,4 cou2.CODE AS countryCode 5 FROM6 t_unionpay_areacode cou1 7 LEFT JOIN t_unionpay_areacode cou2 8 ON cou2.parent_code = cou1.CODE 9 WHERE cou1.NAME = '安阳市' ;
3,两张表在左外连接,通过市名称相同
3.1,连接语句
1 SELECT 2 prov.NAME AS province, 3 prov.CODE AS provinceCode, 4 cti.NAME AS city, 5 cti.CODE AS cityCode, 6 country.country AS country, 7 country.countryCode AS countryCode 8 FROM 9 t_zone_info prov 10 LEFT JOIN t_zone_info cti 11 ON prov.id = cti.parentId 12 LEFT JOIN 13 (SELECT 14 cou1.NAME AS city,15 cou2.NAME AS country,16 cou2.CODE AS countryCode 17 FROM18 t_unionpay_areacode cou1 19 LEFT JOIN t_unionpay_areacode cou2 20 ON cou2.parent_code = cou1.CODE) country 21 ON country.city = cti.NAME 22 WHERE prov.CODE = '410000' 23 ORDER BY province DESC,24 cityCode,25 countryCode
不相同的,或者说表一种的数据有,但是表二中没有的话则用null来显示。