DELIMITER $$
USE `数据库名称`$$
DROP FUNCTION IF EXISTS `rand_county`$$
CREATE DEFINER=`root`@`%` FUNCTION `rand_county`(param_city VARCHAR(64)) RETURNS VARCHAR(128) CHARSET utf8
BEGIN
DECLARE var_city TEXT ;
DECLARE var_county TEXT;
DECLARE var_city_replace TEXT;
DECLARE city_beijing TEXT DEFAULT '北京市,东城区,西城区,朝阳区,丰台区,石景山区,海淀区,顺义区,通州区,大兴区,房山区,门头沟区,昌平区,平谷区,密云区,怀柔区,延庆区';
DECLARE city_tianjin TEXT DEFAULT '天津市,和平区,河东区,河西区,南开区,河北区,红桥区,滨海新区,东丽区,西青区,津南区,北辰区,武清区,宝坻区,宁河区,静海区,蓟州区';
DECLARE city_shanghai TEXT DEFAULT '上海市,黄浦区,徐汇区,长宁区,静安区,普陀区,虹口区,杨浦区,闵行区,宝山区,嘉定区,浦东新区,金山区,松江区,青浦区,奉贤区,崇明区';
DECLARE city_chongqing TEXT DEFAULT '重庆市,渝中区,万州区,涪陵区,大渡口区,江北区,沙坪坝区,九龙坡区,南岸区,北碚区,綦江区,大足区,渝北区,巴南区,黔江区,长寿区,江津区,合川区,永川区,南川区,璧山区,铜梁区,潼南区,荣昌区,开州区,梁平区,武隆区,城口县,丰都县,垫江县,忠县,云阳县,奉节县,巫山县,巫溪县,石柱土家族自治县,秀山土家族苗族自治县,酉阳土家族苗族自治县,彭水苗族土家族自治县';
DECLARE city_henan TEXT DEFAULT '河南省,郑州市,洛阳市,焦作市,商丘市,信阳市,周口市,鹤壁市,安阳市,濮阳市,驻马店市,南阳市,开封市,漯河市,许昌市,新乡市,济源市,灵宝市,偃师市,邓州市,登封市,三门峡市,新郑市,禹州市,巩义市,永城市,长葛市,义马市,林州市,项城市,汝州市,荥阳市,平顶山市,卫辉市,辉县市,舞钢市,新密市,孟州市,沁阳市,郏县';
DECLARE city_anhui TEXT DEFAULT '安徽省,合肥市,亳州市,芜湖市,马鞍山市,池州市,黄山市,滁州市,安庆市,淮南市,淮北市,蚌埠市,宿州市,宣城市,六安市,阜阳市,铜陵市,明光市,天长市,宁国市,界首市,桐城市,潜山市';
DECLARE city_fujian TEXT DEFAULT '福建省,福州市,厦门市,泉州市,漳州市,南平市,三明市,龙岩市,莆田市宁德市,龙海市,建瓯市,武夷山市,长乐市,福清市,晋江市,南安市,福安市,邵武市,石狮市,福鼎市,建阳市,漳平市,永安市';
DECLARE city_gansu TEXT DEFAULT '甘肃省,兰州市,白银市,武威市,金昌市,平凉市,张掖市,嘉峪关市,酒泉市,庆阳市,定西市,陇南市,天水市,玉门市,临夏市,合作市,敦煌市,甘南州';
DECLARE city_guizhou TEXT DEFAULT '贵州省,贵阳市,安顺市,遵义市,六盘水市,兴义市,都匀市,凯里市,毕节市,清镇市,铜仁市,赤水市,仁怀市,福泉市';
DECLARE city_hainan TEXT DEFAULT '海南省,海口市,三亚市,万宁市,文昌市,儋州市,琼海市,东方市,五指山市';
DECLARE city_hebei TEXT DEFAULT '河北省,石家庄市,保定市,唐山市,邯郸市邢台市,沧州市,衡水市,廊坊市,承德市,迁安市,鹿泉市,秦皇岛市,南宫市,任丘市,叶城市,辛集市,涿州市,定州市,晋州市,霸州市,黄骅市,遵化市,张家口市,沙河市,三河市,冀州市,武安市,河间市深州市,新乐市,泊头市,安国市,双滦区,高碑店市';
DECLARE city_heilongjiang TEXT DEFAULT '黑龙江省,哈尔滨市,伊春市,牡丹江市,大庆市,鸡西市,鹤岗市,绥化市,齐齐哈尔市,黑河市,富锦市,虎林市,密山市,佳木斯市,双鸭山市,海林市,铁力市,北安市,五大连池市,阿城市,尚志市,五常市,安达市,七台河市,绥芬河市,双城市,海伦市,宁安市,讷河市,穆棱市,同江市,肇东市';
DECLARE city_hubei TEXT DEFAULT '湖北省,武汉市,荆门市,咸宁市,襄阳市,荆州市,黄石市,宜昌市,随州市,鄂州市,孝感市,黄冈市,十堰市,枣阳市,老河口市,恩施市,仙桃市,天门市,钟祥市,潜江市,麻城市,洪湖市,汉川市,赤壁市,松滋市,丹江口市,武穴市,广水市,石首市大冶市,枝江市,应城市,宜城市,当阳市,安陆市,宜都市,利川市';
DECLARE city_hunan TEXT DEFAULT '湖南省,长沙市,郴州市,益阳市,娄底市,株洲市,衡阳市,湘潭市,岳阳市,常德市,邵阳市,永州市,张家界市,怀化市,浏阳市,醴陵市,湘乡市,耒阳市,沅江市,涟源市,常宁市,吉首市,津市市,冷水江市,临湘市,汨罗市,武冈市,韶山市,湘西州';
DECLARE city_jilin TEXT DEFAULT '吉林省,长春市,吉林市,通化市,白城市,四平市,辽源市,松原市,白山市,集安市,梅河口市,双辽市,延吉市,九台市,桦甸市,榆树市,蛟河市,磐石市,大安市,德惠市,洮南市,龙井市,珲春市,公主岭市,图们市,舒兰市,和龙市,临江市,敦化市';
DECLARE city_jiangsu TEXT DEFAULT '江苏省,南京市,无锡市,常州市,扬州市,徐州市,苏州市,连云港市,盐城市,淮安市,宿迁市,镇江市,南通市,泰州市,兴化市,东台市,常熟市,江阴市,张家港市,通州市,宜兴市,邳州市,海门市,溧阳市,泰兴市,如皋市,昆山市,启东市,江都市,丹阳市,吴江市,靖江市,扬中市,新沂市,仪征市,太仓市,姜堰市,高邮市,金坛市,句容市,灌南县,海安市';
DECLARE city_jiangxi TEXT DEFAULT '江西省,南昌市,赣州市,上饶市,宜春市,景德镇市,新余市,九江市,萍乡市,抚州市,鹰潭市,吉安市,丰城市,樟树市,德兴市,瑞金市,井冈山市,高安市,乐平市,南康市,贵溪市,瑞昌市,东乡县,广丰县,信州区,三清山';
DECLARE city_liaoning TEXT DEFAULT '辽宁省,沈阳市,葫芦岛市,大连市,盘锦市,鞍山市,铁岭市,本溪市,丹东市,抚顺市,锦州市,辽阳市,阜新市,调兵山市,朝阳市,海城市,北票市,盖州市,凤城市,庄河市,凌源市,开原市,兴城市,新民市,大石桥市,东港市,北宁市,瓦房店市,普兰店市,凌海市,灯塔市,营口市';
DECLARE city_qinghai TEXT DEFAULT '青海省,西宁市,格尔木市,德令哈市';
DECLARE city_shandong TEXT DEFAULT '山东省,济南市,青岛市,威海市,潍坊市,菏泽市,济宁市,东营市烟台市,淄博市,枣庄市,泰安市,临沂市,日照市,德州市,聊城市,滨州市,乐陵市,兖州市,诸城市,邹城市,滕州市,肥城市,新泰市,胶州市,胶南市,龙口市,平度市,莱西市';
DECLARE city_shanxi1 TEXT DEFAULT '山西省,太原市,大同市,阳泉市,长治市,临汾市,晋中市,运城市,忻州市,朔州市,吕梁市,古交市,高平市,永济市,孝义市,侯马市,霍州市,介休市,河津市,汾阳市,原平市,晋城市,潞城市';
DECLARE city_shanxi3 TEXT DEFAULT '陕西省,西安市,咸阳市,榆林市,宝鸡市,铜川市,渭南市,汉中市,安康市,商洛市,延安市,韩城市,兴平市,华阴市';
DECLARE city_sichuan TEXT DEFAULT '四川省,成都市,广安市,德阳市,乐山市,巴中市,内江市,宜宾市,南充市,都江堰市,自贡市,泸州市,广元市达州市,资阳市,绵阳市,眉山市,遂宁市,雅安市,阆中市,攀枝花市,广汉市,绵竹市,万源市,华蓥市,江油市,西昌市,彭州市,简阳市,崇州市,什邡市,峨眉山市,邛崃市,双流县';
DECLARE city_yunnan TEXT DEFAULT '云南省,昆明市,玉溪市,大理市,曲靖市,昭通市,保山市,丽江市,临沧市,楚雄市,开远市,个旧市,景洪市,安宁市,宣威市,文山市';
DECLARE city_zhejiang TEXT DEFAULT '浙江省,杭州市,宁波市,绍兴市,温州市,台州市,湖州市,嘉兴市,金华市,舟山市,衢州市,丽水市,余姚市,乐清市,临海市,温岭市,永康市,瑞安市,慈溪市,义乌市,上虞市,诸暨市,海宁市,桐乡市,兰溪市,龙泉市,建德市,富德市,富阳市,平湖市,东阳市,嵊州市,奉化市,临安市,江山市';
DECLARE city_taiwan TEXT DEFAULT '台湾省,台北市,台南市,台中市,高雄市,桃源市';
DECLARE city_guangdong TEXT DEFAULT '广东省,广州市,深圳市,珠海市,汕头市,佛山市,韶关市,湛江市,肇庆市,江门市,茂名市,惠州市,梅州市,汕尾市,河源市,阳江市,清远市,东莞市,中山市,潮州市,揭阳市,云浮市';
DECLARE city_guangxi TEXT DEFAULT '广西壮族自治区,南宁市,贺州市,玉林市,桂林市,柳州市,梧州市,北海市,钦州市,百色市,防城港市,贵港市,河池市,崇左市,来宾市,东兴市,桂平市,北流市,岑溪市,合山市,凭祥市,宜州市';
DECLARE city_neimenggu TEXT DEFAULT '内蒙古自治区,呼和浩特市,呼伦贝尔市,赤峰市,扎兰屯市,鄂尔多斯市,乌兰察布市,巴彦淖尔市,二连浩特市,霍林郭勒市,包头市,乌海市,阿尔山市,乌兰浩特市,锡林浩特市,根河市,满洲里市,额尔古纳市,牙克石市,临河市,丰镇市,通辽市';
DECLARE city_ningxia TEXT DEFAULT '宁夏回族自治区,银川市,固原市,石嘴山市,青铜峡市,中卫市,吴忠市,灵武市';
DECLARE city_xizang TEXT DEFAULT '西藏自治区,拉萨市,那曲市,山南市,林芝市,昌都市,阿里地区日喀则市';
DECLARE city_xinjiang TEXT DEFAULT '新疆维吾尔自治区,乌鲁木齐市,石河子市,喀什市,阿勒泰市,阜康市,库尔勒市,阿克苏市,阿拉尔市,哈密市,克拉玛依市,昌吉市,奎屯市,米泉市,和田市,塔城市';
DECLARE city_xianggang TEXT DEFAULT '香港特别行政区,香港岛,九龙半岛,新界';
DECLARE city_aomen TEXT DEFAULT '澳门特别行政区,花地玛堂区(北区),圣安多尼堂区(花王堂区),大堂区,望德堂区,嘉模堂区,圣方济各堂区,无堂区划分区域,路氹城';
IF LOCATE(param_city,city_henan) THEN SET var_city= city_henan ; SET var_city_replace=REPLACE(city_henan,',','');
ELSEIF LOCATE(param_city,city_anhui ) THEN SET var_city= city_anhui ; SET var_city_replace=REPLACE(city_anhui,',','') ;
ELSEIF LOCATE(param_city,city_fujian ) THEN SET var_city= city_fujian ; SET var_city_replace=REPLACE(city_fujian,',','') ;
ELSEIF LOCATE(param_city,city_gansu) THEN SET var_city= city_gansu ; SET var_city_replace=REPLACE(city_gansu,',','') ;
ELSEIF LOCATE(param_city,city_guizhou ) THEN SET var_city= city_guizhou ; SET var_city_replace=REPLACE(city_guizhou,',','') ;
ELSEIF LOCATE(param_city,city_hainan) THEN SET var_city= city_hainan ; SET var_city_replace=REPLACE(city_hainan,',','') ;
ELSEIF LOCATE(param_city,city_hebei) THEN SET var_city= city_hebei ; SET var_city_replace=REPLACE(city_hebei,',','') ;
ELSEIF LOCATE(param_city,city_heilongjiang) THEN SET var_city= city_heilongjiang ; SET var_city_replace=REPLACE(city_heilongjiang,',','') ;
ELSEIF LOCATE(param_city,city_hubei ) THEN SET var_city= city_hubei ; SET var_city_replace=REPLACE(city_hubei,',','') ;
ELSEIF LOCATE(param_city,city_hunan ) THEN SET var_city= city_hunan ; SET var_city_replace=REPLACE(city_hunan,',','') ;
ELSEIF LOCATE(param_city,city_jilin) THEN SET var_city= city_jilin ; SET var_city_replace=REPLACE(city_jilin,',','') ;
ELSEIF LOCATE(param_city,city_jiangsu ) THEN SET var_city= city_jiangsu ; SET var_city_replace=REPLACE(city_jiangsu,',','') ;
ELSEIF LOCATE(param_city,city_jiangxi ) THEN SET var_city= city_jiangxi ; SET var_city_replace=REPLACE(city_jiangxi,',','') ;
ELSEIF LOCATE(param_city,city_liaoning ) THEN SET var_city= city_liaoning ; SET var_city_replace=REPLACE(city_liaoning,',','') ;
ELSEIF LOCATE(param_city,city_qinghai) THEN SET var_city= city_qinghai ; SET var_city_replace=REPLACE(city_qinghai,',','') ;
ELSEIF LOCATE(param_city,city_shandong ) THEN SET var_city=city_shandong ; SET var_city_replace=REPLACE(city_shandong,',','') ;
ELSEIF LOCATE(param_city,city_shanxi1 ) THEN SET var_city=city_shanxi1 ; SET var_city_replace=REPLACE(city_shanxi1,',','') ;
ELSEIF LOCATE(param_city,city_shanxi3 ) THEN SET var_city= city_shanxi3 ; SET var_city_replace=REPLACE(city_shanxi3,',','') ;
ELSEIF LOCATE(param_city,city_sichuan) THEN SET var_city= city_sichuan ; SET var_city_replace=REPLACE(city_sichuan,',','') ;
ELSEIF LOCATE(param_city,city_yunnan ) THEN SET var_city= city_yunnan ; SET var_city_replace=REPLACE(city_yunnan,',','') ;
ELSEIF LOCATE(param_city,city_zhejiang ) THEN SET var_city= city_zhejiang ; SET var_city_replace=REPLACE(city_zhejiang,',','') ;
ELSEIF LOCATE(param_city,city_taiwan ) THEN SET var_city= city_taiwan ; SET var_city_replace=REPLACE(city_taiwan,',','') ;
ELSEIF LOCATE(param_city,city_guangdong ) THEN SET var_city= city_guangdong ; SET var_city_replace=REPLACE(city_guangdong,',','') ;
ELSEIF LOCATE(param_city,city_guangxi ) THEN SET var_city= city_guangxi ; SET var_city_replace=REPLACE(city_guangxi,',','') ;
ELSEIF LOCATE(param_city,city_neimenggu ) THEN SET var_city= city_neimenggu ; SET var_city_replace=REPLACE(city_neimenggu,',','') ;
ELSEIF LOCATE(param_city,city_ningxia ) THEN SET var_city=city_ningxia ; SET var_city_replace=REPLACE(city_ningxia,',','');
ELSEIF LOCATE(param_city,city_xizang ) THEN SET var_city= city_xizang ; SET var_city_replace=REPLACE(city_xizang,',','') ;
ELSEIF LOCATE(param_city,city_xinjiang ) THEN SET var_city= city_xinjiang ; SET var_city_replace=REPLACE(city_xinjiang,',','') ;
ELSEIF LOCATE(param_city,city_xianggang ) THEN SET var_city= city_xianggang ; SET var_city_replace=REPLACE(city_xianggang,',','') ;
ELSEIF LOCATE(param_city,city_aomen ) THEN SET var_city= city_aomen ; SET var_city_replace=REPLACE(city_aomen,',','') ;
ELSEIF LOCATE(param_city,city_beijing ) THEN SET var_city= city_beijing ; SET var_city_replace=REPLACE(city_beijing,',','') ;
ELSEIF LOCATE(param_city,city_tianjin ) THEN SET var_city= city_tianjin ; SET var_city_replace=REPLACE(city_tianjin,',','') ;
ELSEIF LOCATE(param_city,city_shanghai ) THEN SET var_city= city_shanghai ; SET var_city_replace=REPLACE(city_shanghai,',','') ;
ELSEIF LOCATE(param_city,city_chongqing ) THEN SET var_city= city_chongqing ; SET var_city_replace=REPLACE(city_chongqing,',','') ;
END IF;
SET @county_num =LENGTH(var_city)-LENGTH(var_city_replace);
SET @rand_county =SUBSTRING_INDEX(var_city,',',-@county_num);
SET var_county =rand_string(@rand_county);
SET var_city=SUBSTRING_INDEX(var_city,',',1);
RETURN CONCAT(var_city,var_county);
END$$
DELIMITER ;
DELIMITER $$
USE `数据库名称`$$
DROP FUNCTION IF EXISTS `rand_string`$$
CREATE DEFINER=`root`@`%` FUNCTION `rand_string`(var_string TEXT) RETURNS TEXT CHARSET utf8
BEGIN
#根据逗号分隔字符串有多少个数
SET @num= LENGTH(var_string)-LENGTH(REPLACE(var_string,',',''))+1;
#随机生成位置
SET @index= FLOOR(RAND()*@num)+1;
SET @rand_start =SUBSTRING_INDEX(var_string,',',@index);
SET @rand_end =SUBSTRING_INDEX(@rand_start,',',-1);
RETURN @rand_end ;
END$$
DELIMITER ;
/*指定省份+随机产生县区*/
SELECT `rand_county`('河北省');
/*随机省份*/
SELECT `rand_city`();
/*随机省份+随机所在县区*/
SELECT `rand_county`(`rand_city`());