怎么用concat批量生成MySQL查询语句

这篇文章主要介绍“怎么用concat批量生成MySQL查询语句”,在日常操作中,相信很多人在怎么用concat批量生成MySQL查询语句问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用concat批量生成MySQL查询语句”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

需求背景:
一个库有2个用户,比如gsgz和gxsnerp。
gsgz的用户有这个库的所有权限,而gxsnerp这个用户只有部分表的权限,所以不能直接用grant all on ...这个语句来给权限,稍微快一点的方法就是批量生成拼接的SQL语句。

select concat('grant all on ', table_schema,".",table_name,"  to ' gxsnerp'@'localhost ';") from tables where table_schema='gxsnerp' and table_name like 'ACT%';

生成拼接语句如下:

grant all on  gxsnerp.ACT_HI_VARINST` to 'gxsnerp'@'localhost';      
grant all  on gxsnerp.ACT_GE_PROPERTY to 'gxsnerp'@'localhost';     
grant all  on gxsnerp.ACT_ID_MEMBERSHIP to 'gxsnerp'@'localhost' ;  
grant all  on gxsnerp.ACT_HI_ACTINST to 'gxsnerp'@'localhost' ;

到此,关于“怎么用concat批量生成MySQL查询语句”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!