问题描述
现在我有两张表 T1为 company表 address(详细地址) PS: 中国 浙江 杭州市 拱墅区...路...号....provinceId;//省IdcityId;//市IdzoneId;//区Idprovince;//省city;//市zone;//区T2表为 省市区地址及IDname ps:全国的地址(省市区全有) parentId ps:每个省市区 对应的Id如何将T1表中的address根据空格拆分出各个省市区 存入province,city,zone在根据T2表找到T1表中province,city,zone找到对应的ID 存入T1表中对应的provinceId,cityId,zoneId(数据量很大70W的数据量)如何实现呢? 辛苦大神们 帮小弟解决下,小弟在此谢过了! 问题补充:MySQl数据库 还是sql实现吧,数据量有点大,我怕java后台内存泄漏,你要是方便的话,都实现下吧,我也想学学java处理大量数据时怎么做比较好hudingchen 写道
解决方案
表结构和数据:mysql> desc t1;+------------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+------------+--------------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || provinceid | varchar(45) | YES | | NULL | || cityid | varchar(45) | YES | | NULL | || zoneid | varchar(45) | YES | | NULL | || province | varchar(45) | YES | | NULL | || city | varchar(45) | YES | | NULL | || zone | varchar(45) | YES | | NULL | || address | varchar(200) | YES | | NULL | |+------------+--------------+------+-----+---------+----------------+mysql> desc t2;+----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || name | varchar(45) | YES | | NULL | || parentld | varchar(45) | YES | | NULL | |+----------+-------------+------+-----+---------+----------------+mysql> select * from t1;+----+------------+--------+--------+----------+------+------+-------------------------------------------+| id | provinceid | cityid | zoneid | province | city | zone | address |+----+------------+--------+--------+----------+------+------+-------------------------------------------+| 1 | | | | | NULL | NULL | 中国 浙江1 杭州市1 拱墅区1 2路 || 2 | NULL | NULL | NULL | | NULL | NULL | 中国 浙江2 杭州市2 拱墅区2 2路 |+----+------------+--------+--------+----------+------+------+-------------------------------------------+2 rows in set (0.00 sec)mysql> select * from t2;+----+------------+----------+| id | name | parentld |+----+------------+----------+| 1 | 浙江1 | p01 || 2 | 浙江2 | p02 || 3 | 杭州市1 | c01 || 4 | 杭州市2 | c02 || 5 | 拱墅区1 | z01 || 6 | 拱墅区2 | z02 |+----+------------+----------+6 rows in set (0.00 sec)存储过程:CREATE FUNCTION func_get_split_string(f_string varchar(1000),f_delimiter varchar(5),f_order int) RETURNS varchar(255) CHARSET utf8BEGIN declare result varchar(255) default ''; set result = reverse(substring_index(reverse(substring_index(f_string,f_delimiter,f_order)),f_delimiter,1)); return result;ENDCREATE PROCEDURE split_data()BEGIN DECLARE tid int DEFAULT 0; DECLARE address_name VARCHAR(200) DEFAULT ""; DECLARE province_name VARCHAR(200) DEFAULT ""; DECLARE city_name VARCHAR(200) DEFAULT ""; DECLARE zone_name VARCHAR(200) DEFAULT ""; DECLARE province_id VARCHAR(200) DEFAULT ""; DECLARE city_id VARCHAR(200) DEFAULT ""; DECLARE zone_id VARCHAR(200) DEFAULT ""; DECLARE done INT DEFAULT 0; DECLARE cur CURSOR FOR SELECT id,address FROM t1; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; OPEN cur; address_lb:LOOP FETCH cur INTO tid,address_name; SELECT func_get_split_string(address_name,' ',2) INTO province_name; SELECT func_get_split_string(address_name,' ',3) INTO city_name; SELECT func_get_split_string(address_name,' ',4) INTO zone_name; SELECT parentld INTO province_id FROM t2 WHERE name = province_name; SELECT parentld INTO city_id FROM t2 WHERE name = city_name; SELECT parentld INTO zone_id FROM t2 WHERE name = zone_name; UPDATE t1 SET province = province_name, city = city_name, ZONE = zone_name, provinceid = province_id, cityid = city_id, zoneid = zone_id WHERE id = tid; IF done = 1 THEN LEAVE address_lb; END IF; END LOOP address_lb; CLOSE cur; commit;END执行:mysql> call split_data();Query OK, 0 rows affected (0.00 sec)mysql> select * from t1;+----+------------+--------+--------+----------+------------+------------+-------------------------------------------+| id | provinceid | cityid | zoneid | province | city | zone | address |+----+------------+--------+--------+----------+------------+------------+-------------------------------------------+| 1 | p01 | c01 | z01 | 浙江1 | 杭州市1 | 拱墅区1 | 中国 浙江1 杭州市1 拱墅区1 2路 || 2 | p02 | c02 | z02 | 浙江2 | 杭州市2 | 拱墅区2 | 中国 浙江2 杭州市2 拱墅区2 2路 |+----+------------+--------+--------+----------+------------+------------+-------------------------------------------+2 rows in set (0.00 sec)
解决方案二:
做关联呀,做关系表就行
解决方案三:
考虑效率的话,最好写mysql 存储过程,说下思路:1.遍历T1表。2.取出address字段,根据空格解析address(详细地址),不会的话搜索下:mysql split。3.根据2解析的数据,更新T1表。
解决方案四:
http://topic.csdn.net/t/20060922/14/5041077.html
解决方案五:
http://topic.csdn.net/t/20060922/14/5041077.html
解决方案六:
什么数据库?你想sql实现?还是java后台实现?