原文链接:mysql之concat函数批量操作
前几天主管给我说,有一笔数据有误,让我捞取更新一下,我默默的点了点头说好,查询了一下数据库,几万笔数据,怎么改?我就懵掉了,磨磨唧唧实在没办法,催的又急。
我便说我这链接工具有问题,让主管帮我改一下,呆在旁边,便看见这个函数concat,
~~哈哈,将函数写在手心学习记载一下。
语法:concat(‘str1’,‘str2’,‘str’)
函数使用说明:
返回不同的非NULL 值数目。若找不到匹配的项,则COUNT(DISTINCT) 返回 0项。
1、concat函数拼接如下,拼接完成123。
select concat('1','2','3') from resource_file;
-- 结果集
concat('1','2','3')
123
123
...
具体条数,依据查询的条数生成。
2、当查询拼接值为空时,返回值为空。
select concat('1','2','null','3') from resource_file;
-- 结果集
concat('1','2','null','3')
null
null
...
3、concat的sql注入。
批量操作注入sql语句,用于批量更新,插入等操作。
如上查询的结果集,需要保证查询的结果集在其中。
将concat函数替换*。
select
concat('update rrcba set rrcba05="',rrcga10,'" where id="',cba.id,'";')
-- 这里是更换*的concat函数,里面数据来源于结果集。
from
( select rrcb14, id, rrcb02 from rrcb where rrcb14 not like '%yjgoodjob%' and rrcb06 = 'HC' ) t
left join rrcba cba on rrcb02 = rrcba02
left join rrcga on rrcga.id = rrcb14
where
rrcba04 = '1'
运行sql便会完成多条更新语句,将其复制出来执行。
运行结果如下:
update rrcba set rrcba05="2019-01-23 10:09:00" where id="ff8080816989985d016989b0ea60009c";
update rrcba set rrcba05="2019-01-23 10:09:00" where id="ff8080816989985d016989b269c500a6";
update rrcba set rrcba05="2019-01-23 10:09:00" where id="ff8080816989985d016989b2a66600a7";
update rrcba set rrcba05="2019-01-23 10:09:00" where id="ff8080816989985d016989b2cdd400a8";
update rrcba set rrcba05="2019-01-23 10:09:00" where id="ff8080816989985d016989b2f83700aa";
update rrcba set rrcba05="2019-01-23 10:09:00" where id="ff8080816989985d016989b315a500ac";
update rrcba set rrcba05="2019-01-23 10:09:00" where id="ff8080816989985d016989b3322200af";
update rrcba set rrcba05="2019-01-23 10:09:00" where id="ff8080816989985d016989b350ee00b2";
update rrcba set rrcba05="2019-01-22 09:07:00" where id="ff8080816989985d016989b36e0900b4";
update rrcba set rrcba05="2019-01-22 09:07:00" where id="ff8080816989985d016989b38c3600b6";
update rrcba set rrcba05="2019-01-22 09:07:00" where id="ff8080816989985d016989b3ab9700b7";
update rrcba set rrcba05="2019-01-22 09:07:00" where id="ff8080816989985d016989b3caba00b9";
update rrcba set rrcba05="2019-01-22 09:07:00" where id="ff8080816989985d016989b3ee2e00ba";
update rrcba set rrcba05="2019-01-22 09:07:00" where id="ff8080816989985d016989b40e2a00bb";
在同张表内的字段直接update更新,这样的方式,适合不同表之间的集合。
面向开发过程,记录学习之路。