MySQL 批量生成 SQL 脚本语句解决实际的业务需求/如何拼接字符串/拼接字符串的 SQL 语句

文章目录

  • 实际需求
  • 分析思路
  • 写拼接 SQL 脚本的脚本语句
  • 执行得到脚本语句
  • 保存成 SQL 脚本文件

实际需求

有些行政区域的字段 area_fullname 是空的,如何补全呢?如下所示:
在这里插入图片描述

分析思路

(一)如何取到每个区域的上级名称和上上级名称?
区域编码有规律,末尾有2个0的是上级,末尾有3个0 的是上上级,从而可以通过截取字符串、拼接字符串、关联子查询来得到。

(二)用到哪些技术点?

  1. 截取字符串函数
  2. 拼接字符串函数
  3. 空值转换函数
  4. 关联子查询
上级:(ifnull((select b.area_name from td_area_test b where b.area_code 
= concat(left(a.area_code,4),'00')),'')) superior上上级:(ifnull((select c.area_name from td_area_test c where c.area_code 
= concat(left(a.area_code,3),'000')),'')) superlative本级:a.area_name inferior

把 area_fullname 为空的区域的名称,上级名称,上上级名称查询出来,语句如下:

select a.area_code,a.area_name as inferior,
(ifnull((select b.area_name from td_area_test b 
where b.area_code = concat(left(a.area_code,4),'00')),'')) superior,
(ifnull((select c.area_name from td_area_test c 
where c.area_code = concat(left(a.area_code,3),'000')),'')) superlative 
from td_area_test a where a.area_fullname ='';

写拼接 SQL 脚本的脚本语句

批量生成 SQL 脚本语句的脚本语句如下:

select concat('update td_area_test set area_fullname = ''',(concat((ifnull((select c.area_name from td_area_test c
where c.area_code = concat(left(a.area_code,3),'000')),'')),(ifnull((select b.area_name from td_area_test b where
b.area_code = concat(left(a.area_code,4),'00')),'')),a.area_name)),''' where area_code = ''',a.area_code,''';')
sqlsentence from td_area_test a where a.area_fullname = '';

执行得到脚本语句

执行上述脚本语句后得到如下结果:

mysql> select concat('update td_area_test set area_fullname = ''',(concat((ifnull((select c.area_name from td_area_test c where c.area_code = concat(left(a.area_code,3),'000')),'')),(ifnull((select b.area_name from td_area_test b where b.area_code = concat(left(a.area_code,4),'00')),'')),a.area_name)),''' where area_code = ''',a.area_code,''';') sqlsentence from td_area_test a where a.area_fullname = '';
+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| sqlsentence                                                                                                                                             |
+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| update td_area_test set area_fullname = '内蒙古自治区赤峰市敖汉旗' where area_code = '150430';                                                          |
| update td_area_test set area_fullname = '锡林郭勒盟正蓝旗' where area_code = '152530';                                                                  |
| update td_area_test set area_fullname = '黑龙江省哈尔滨市香坊区' where area_code = '230110';                                                            |
| update td_area_test set area_fullname = '黑龙江省齐齐哈尔市克东县' where area_code = '230230';                                                          |
| update td_area_test set area_fullname = '黑龙江省伊春市五营区' where area_code = '230710';                                                              |
| update td_area_test set area_fullname = '上海市杨浦区' where area_code = '310110';                                                                      |
| update td_area_test set area_fullname = '上海市奉贤区' where area_code = '310120';                                                                      |
| update td_area_test set area_fullname = '上海市崇明县' where area_code = '310230';                                                                      |
| update td_area_test set area_fullname = '江苏省淮安市盱眙县' where area_code = '320830';                                                                |
| update td_area_test set area_fullname = '浙江省杭州市余杭区' where area_code = '330110';                                                                |
| update td_area_test set area_fullname = '罗津' where area_code = '342201';                                                                              |
| update td_area_test set area_fullname = '福建省三明市建宁县' where area_code = '350430';                                                                |
| update td_area_test set area_fullname = '江西省九江市彭泽县' where area_code = '360430';                                                                |
| update td_area_test set area_fullname = '江西省赣州市宁都县' where area_code = '360730';                                                                |
| update td_area_test set area_fullname = '江西省吉安市永新县' where area_code = '360830';                                                                |
| update td_area_test set area_fullname = '抚州市抚州市广昌县' where area_code = '361030';                                                                |
| update td_area_test set area_fullname = '抚州市上饶市婺源县' where area_code = '361130';                                                                |
| update td_area_test set area_fullname = '山东省济宁市汶上县' where area_code = '370830';                                                                |
| update td_area_test set area_fullname = '许昌市南阳市桐柏县' where area_code = '411330';                                                                |
| update td_area_test set area_fullname = '湖南省长沙市安居区' where area_code = '430185';                                                                |
| update td_area_test set area_fullname = '湖南省株洲市芦松区' where area_code = '430282';                                                                |
| update td_area_test set area_fullname = '湖南省常德市贺家山原种场' where area_code = '430782';                                                          |
| update td_area_test set area_fullname = '湖南省常德市德山开发区' where area_code = '430783';                                                            |
| update td_area_test set area_fullname = '湖南省常德市西湖管理区' where area_code = '430784';                                                            |
| update td_area_test set area_fullname = '湖南省常德市西洞庭管理区' where area_code = '430785';                                                          |
| update td_area_test set area_fullname = '湖南省益阳市大通湖区' where area_code = '430940';                                                              |
| update td_area_test set area_fullname = '郴州市怀化通道侗族自治县' where area_code = '431230';                                                          |
| update td_area_test set area_fullname = '郴州市怀化洪江区' where area_code = '431282';                                                                  |
| update td_area_test set area_fullname = '湘西土家族苗族自治州龙山县' where area_code = '433130';                                                        |
| update td_area_test set area_fullname = '广西壮族自治区桂林市平乐县' where area_code = '450330';                                                        |
| update td_area_test set area_fullname = '百色市百色市西林县' where area_code = '451030';                                                                |
| update td_area_test set area_fullname = '省直辖县级行政区划省直辖县级行政区划琼中黎族苗族自治县' where area_code = '469030';                            |
| update td_area_test set area_fullname = '重庆市万盛区' where area_code = '500110';                                                                      |
| update td_area_test set area_fullname = '重庆市丰都县' where area_code = '500230';                                                                      |
| update td_area_test set area_fullname = '重庆市石柱土家族自治县' where area_code = '500240';                                                            |
| update td_area_test set area_fullname = '阿坝藏族羌族自治州壤塘县' where area_code = '513230';                                                          |
| update td_area_test set area_fullname = '甘孜藏族自治州德格县' where area_code = '513330';                                                              |
| update td_area_test set area_fullname = '凉山彝族自治州金阳县' where area_code = '513430';                                                              |
| update td_area_test set area_fullname = '贵州省遵义市习水县' where area_code = '520330';                                                                |
| update td_area_test set area_fullname = '铜仁地区万山特区' where area_code = '522230';                                                                  |
| update td_area_test set area_fullname = '黔东南苗族侗族自治州台江县' where area_code = '522630';                                                        |
| update td_area_test set area_fullname = '黔南布依族苗族自治州龙里县' where area_code = '522730';                                                        |
| update td_area_test set area_fullname = '云南省昭通市水富县' where area_code = '530630';                                                                |
| update td_area_test set area_fullname = '红河哈尼族彝族自治州金平苗族瑶族傣族自治县' where area_code = '532530';                                        |
| update td_area_test set area_fullname = '大理白族自治州洱源县' where area_code = '532930';                                                              |
| update td_area_test set area_fullname = '日喀则地区仁布县' where area_code = '542330';                                                                  |
| update td_area_test set area_fullname = '那曲地区尼玛县' where area_code = '542430';                                                                    |
| update td_area_test set area_fullname = '陕西省宝鸡市凤县' where area_code = '610330';                                                                  |
| update td_area_test set area_fullname = '陕西省咸阳市淳化县' where area_code = '610430';                                                                |
| update td_area_test set area_fullname = '陕西省延安市宜川县' where area_code = '610630';                                                                |
| update td_area_test set area_fullname = '陕西省汉中市佛坪县' where area_code = '610730';                                                                |
| update td_area_test set area_fullname = '陕西省榆林市清涧县' where area_code = '610830';                                                                |
| update td_area_test set area_fullname = '克孜勒苏柯尔克孜自治州喀什地区巴楚县' where area_code = '653130';                                              |
+---------------------------------------------------------------------------------------------------------------------------------------------------------+
53 rows in set (0.01 sec)

保存成 SQL 脚本文件

上述生成的SQL 脚本语句直接从命令行复制粘贴到脚本文件中,要注意清除掉多余的符号 | ,然后在命令终端执行脚本文件即可,清理后如下:

update td_area_test set area_fullname = '内蒙古自治区赤峰市敖汉旗' where area_code = '150430';                                                          
update td_area_test set area_fullname = '锡林郭勒盟正蓝旗' where area_code = '152530';                                                                  
update td_area_test set area_fullname = '黑龙江省哈尔滨市香坊区' where area_code = '230110';                                                           
update td_area_test set area_fullname = '黑龙江省齐齐哈尔市克东县' where area_code = '230230';                                                          
update td_area_test set area_fullname = '黑龙江省伊春市五营区' where area_code = '230710';                                                              
update td_area_test set area_fullname = '上海市杨浦区' where area_code = '310110';                                                                      
update td_area_test set area_fullname = '上海市奉贤区' where area_code = '310120';                                                                      
update td_area_test set area_fullname = '上海市崇明县' where area_code = '310230';                                                                      
update td_area_test set area_fullname = '江苏省淮安市盱眙县' where area_code = '320830';                                                                
update td_area_test set area_fullname = '浙江省杭州市余杭区' where area_code = '330110';                                                                
update td_area_test set area_fullname = '罗津' where area_code = '342201';                                                                              
update td_area_test set area_fullname = '福建省三明市建宁县' where area_code = '350430';                                                                
update td_area_test set area_fullname = '江西省九江市彭泽县' where area_code = '360430';                                                                
update td_area_test set area_fullname = '江西省赣州市宁都县' where area_code = '360730';                                                                
update td_area_test set area_fullname = '江西省吉安市永新县' where area_code = '360830';                                                                
update td_area_test set area_fullname = '抚州市抚州市广昌县' where area_code = '361030';                                                                
update td_area_test set area_fullname = '抚州市上饶市婺源县' where area_code = '361130';                                                                
update td_area_test set area_fullname = '山东省济宁市汶上县' where area_code = '370830';                                                                
update td_area_test set area_fullname = '许昌市南阳市桐柏县' where area_code = '411330';                                                                
update td_area_test set area_fullname = '湖南省长沙市安居区' where area_code = '430185';                                                                
update td_area_test set area_fullname = '湖南省株洲市芦松区' where area_code = '430282';                                                                
update td_area_test set area_fullname = '湖南省常德市贺家山原种场' where area_code = '430782';                                                          
update td_area_test set area_fullname = '湖南省常德市德山开发区' where area_code = '430783';                                                            
update td_area_test set area_fullname = '湖南省常德市西湖管理区' where area_code = '430784';                                                            
update td_area_test set area_fullname = '湖南省常德市西洞庭管理区' where area_code = '430785';                                                          
update td_area_test set area_fullname = '湖南省益阳市大通湖区' where area_code = '430940';                                                              
update td_area_test set area_fullname = '郴州市怀化通道侗族自治县' where area_code = '431230';                                                          
update td_area_test set area_fullname = '郴州市怀化洪江区' where area_code = '431282';                                                                  
update td_area_test set area_fullname = '湘西土家族苗族自治州龙山县' where area_code = '433130';                                                        
update td_area_test set area_fullname = '广西壮族自治区桂林市平乐县' where area_code = '450330';                                                        
update td_area_test set area_fullname = '百色市百色市西林县' where area_code = '451030';                                                                
update td_area_test set area_fullname = '省直辖县级行政区划省直辖县级行政区划琼中黎族苗族自治县' where area_code = '469030';                            
update td_area_test set area_fullname = '重庆市万盛区' where area_code = '500110';                                                                      
update td_area_test set area_fullname = '重庆市丰都县' where area_code = '500230';                                                                      
update td_area_test set area_fullname = '重庆市石柱土家族自治县' where area_code = '500240';                                                            
update td_area_test set area_fullname = '阿坝藏族羌族自治州壤塘县' where area_code = '513230';                                                          
update td_area_test set area_fullname = '甘孜藏族自治州德格县' where area_code = '513330';                                                              
update td_area_test set area_fullname = '凉山彝族自治州金阳县' where area_code = '513430';                                                              
update td_area_test set area_fullname = '贵州省遵义市习水县' where area_code = '520330';                                                                
update td_area_test set area_fullname = '铜仁地区万山特区' where area_code = '522230';                                                                  
update td_area_test set area_fullname = '黔东南苗族侗族自治州台江县' where area_code = '522630';                                                        
update td_area_test set area_fullname = '黔南布依族苗族自治州龙里县' where area_code = '522730';                                                        
update td_area_test set area_fullname = '云南省昭通市水富县' where area_code = '530630';                                                                
update td_area_test set area_fullname = '红河哈尼族彝族自治州金平苗族瑶族傣族自治县' where area_code = '532530';                                        
update td_area_test set area_fullname = '大理白族自治州洱源县' where area_code = '532930';                                                              
update td_area_test set area_fullname = '日喀则地区仁布县' where area_code = '542330';                                                                  
update td_area_test set area_fullname = '那曲地区尼玛县' where area_code = '542430';                                                                    
update td_area_test set area_fullname = '陕西省宝鸡市凤县' where area_code = '610330';                                                                  
update td_area_test set area_fullname = '陕西省咸阳市淳化县' where area_code = '610430';                                                                
update td_area_test set area_fullname = '陕西省延安市宜川县' where area_code = '610630';                                                                
update td_area_test set area_fullname = '陕西省汉中市佛坪县' where area_code = '610730';                                                                
update td_area_test set area_fullname = '陕西省榆林市清涧县' where area_code = '610830';                                                                
update td_area_test set area_fullname = '克孜勒苏柯尔克孜自治州喀什地区巴楚县' where area_code = '653130';   

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/334402.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php的变量都放在哪里,php变量一般放在哪个位置

php变量一般放在哪个位置php定义变量的要求格式,是非常宽松的,至于在哪里定义变量就需要看你的需求,可以在构造函数,也可以在你定义的方法中定义局部变量,也可以在构造函数外面定义全局变量。// 局部变量 函数内部func…

oauth2令牌刷新_了解OAuth2令牌认证

oauth2令牌刷新1.简介 在本教程中,我们将了解OAuth2令牌身份验证 ,以便只有经过身份验证的用户和应用程序才能获得有效的访问令牌,该令牌随后可用于访问服务器上的授权API(在OAuth术语中仅是受保护的资源)。 使用基于…

整型数组 判断 java,给定一个整数数组,判断其中是否有3个数和为N

借助集合将复杂度降到n2,但耗时还是比较长。 import java.util.HashMap;import java.util.HashSet;import java.util.Scanner;/*** Author: coderjjp* Date: 2020-05-07 8:40* Description:给定一个整数数组,判断其中是否有3个数和为N* version: 1.0*/public class …

jsap支付_Java命令行界面(第20部分):JSAP

jsap支付JSAP ( Java Simple Argument Parser )2.1是本系列文章的第二十篇,重点是处理Java的命令行参数。 JSAP页面描述了该库存在的原因:“我在Internet上找到了多个解析器,所有解析器都处理了开关,但是没…

QPW 行政区划字典表(td_area)

行政区划字典表 CREATE TABLE td_area (area_code varchar(10) NOT NULL COMMENT 区域编码,area_name varchar(50) DEFAULT NULL COMMENT 区域名称,area_fullName varchar(300) DEFAULT NULL COMMENT 区域全称,is_hot tinyint(2) DEFAULT 0 COMMENT 是否热门, # 0-否&#xff…

python语句大全input_input提示文字 Python基础输入函数,if-else语句,if-elif

input()函数 此功能用于获取用户输入。 (调用1)input后,程序将立即暂停并等待用户输入。在用户完成内容输入后,单击Enter,程序将继续向下执行。 例如: input() (2&#x…

Linux 命令之 lsusb -- 显示本机的USB设备列表信息

文章目录命令介绍常用选项命令示例(一)显示 USB 设备详细信息命令介绍 lsusb命令用于显示本机的USB设备列表,以及USB设备的详细信息。 lsusb命令显示的USB设备信息来自“/proc/bus/usb”目录下的对应文件。 语法格式:lsusb [选项…

ftp限流java,FTP流量限制的方法

一般来说,下载都是通过FTP来实现的,这样简单的采用ACLs就可以实现的。不过这样存在一个问题,就是原来正常的网络访问也给禁止了,无法继续工作,另外,还有大量的DOWNLOAD不通过FTP,而是借助HTTP协…

argparser_Java命令行界面(第22部分):argparser

argparserJohn Lloyd的argparser是本系列的第二十二篇有关基于Java的命令行参数解析的文章中介绍的库。 该库的主页除了提供单个源代码示例外,还提供了指向基于Javadoc的API文档 ,JAR文件,ZIP文件和TAR文件的链接。 本帖子中使用的示例与本系…

判断 小程序 是否 滚动到页面底部 scrolltolower_微信小程序长列表性能优化——recycle-view

背景:第七次人口普查项目使用是微信小程序原生框架,组件是根据用户需求由项目组前端组组长封装完成的。采集小程序正式登记首页列表页面,根据腾讯老哥在sentry上的监控可以看出,列表页面前端性能比较差,主要表现在一些…

Linux 命令之 lspci -- 显示当前设备所有PCI总线信息

文章目录命令介绍常用选项命令示例(一)罗列 PCI 设备命令介绍 lspci命令用于显示当前主机的所有PCI总线信息,以及所有已连接的PCI设备信息。 现在主流设备如网卡储存等都采用PCI总线 常用选项 选项说明-n以数字方式显示PCI厂商和设备代码-…

arm java 性能怎么样,ARM v6上使用java的Number to String转换性能

我在ARM v6处理器上运行Java软件.这个程序的性质要求我将一些数字(int或float)转换为String.处理器运行速度为850Mhz. Java Runtime是OpenJDK Zero VM 1.7.0_21-b02.我并不期待这里有坚如磐石的表演,但我希望能比我在下面的代码片段中看到的更有效.long time1, time2;float[] s…

java rop_Java命令行界面(第23部分):Rop

java ropRop库在其主页上被描述为“用Java编写的轻量级命令行选项解析器”。 Rop的“简介”还指出:“ Rop的设计目的是最小化同时方便,并涵盖了大多数常见的命令行解析用例。” 这篇文章是本系列中有关解析Java命令行参数的系列文章中的第23部分&#xf…

python2打开文件_关于python:何时以二进制模式打开文件(b)?

我注意到在文档中他们总是用wb打开一个CSV文件。 为什么b? 我知道b代表二进制模式,但是你什么时候使用二进制模式(我猜想CSV文件不是二进制模式)。 如果相关我是从arcpy.da.SearchCursor()查询的结果写入CSV 编辑:根据这个答案注意到wb用于编…

Linux 命令之 dmidecode -- 显示机器的DMI信息

文章目录命令介绍常用选项(一)Valid string keywords are(二)Valid type keywords are(三)type全部编码列表命令示例示例1,-d 后面跟任何东西,输出内容都相同,奇葩&#…

java 接口 私有_Java 9:好的,坏的和私有的接口方法

java 接口 私有Java 9 是在几周前发布的。 查看发行说明 ,其中包含许多有趣的功能。 不过,我觉得并非一切都是不如Oracle和Java行家似乎图片吧 。 我看到了Java世界中的三个趋势,分别是好,坏和丑陋。 让我们从好的开始。 Birdman…

java群面自我介绍,腾讯群面,自我介绍很重要

我面试时间是十一点,但是一直等到十二点多才开始,去了以后先要填一张表,贴照片,内容差不多就是我们自己简历上的,然后再群面的时候交给面试官。我们那个小组是六个人,在一个房间里,面试官先让我…

python卸载module_Python学习笔记

拖了一整年终于开始学习Python编程。为了逼自己快速上路,强行要求自己本学期的两门课程全部的coding作业用Python完成。 一门机器学习(computational Stats),一门Jeff WU 大佬的实验设计与分析(DOE)。即使R…

Linux 命令之 uname -- 显示系统/主机的相关信息

文章目录一、命令介绍二、常用选项三、命令示例(一)显示系统主机名、内核版本号、CPU类型等信息一、命令介绍 uname 命令的英文全称即“Unix name”。用于显示系统相关信息,比如主机名、内核版本号、硬件架构等。如果未指定任何选项&#xf…

json api_JSON模式在构建和部署API中的作用

json api什么是JSON模式 ? 它提供了一种描述任何JSON值的结构和属性的彻底方法。 在记录对任何JSON API的请求和响应时,它非常有用。 本文将探讨其在API的软件开发周期中的作用。 记录JSON响应格式 定义数据架构的最明显的用例也许是在记录API响应的结构…