工作中经常需要批处理数据,select 字符串连接生成SQL from tab;
而对字符串的字段,值2边通常需要单引号,在select concat(字符串1,字符串2..........)时,给字符串加单引号,目前知道的有如下方法:
基于select 'test',给test2边添加单引号
select '\'test\''; #用反斜杠转义
select '''test'''; #''一起变成一个单引号
select concat("'",'test',"'"); #用""去意
select QUOTE('test'); #使用QUOTE函数
实验:
mysql> select 'test';
+------+
| test |
+------+
| test |
+------+
1 row in set (0.00 sec)
mysql> select '\'test\'';
+--------+
| 'test' |
+--------+
| 'test' |
+--------+
1 row in set (0.00 sec)
mysql> select '''test''';
+--------+
| 'test' |
+--------+
| 'test' |
+--------+
1 row in set (0.00 sec)
mysql> select concat("'",'test',"'");
+------------------------+
| concat("'",'test',"'") |
+------------------------+
| 'test' |
+------------------------+
1 row in set (0.00 sec)
mysql> select QUOTE('test');
+---------------+
| QUOTE('test') |
+---------------+
| 'test' |
+---------------+
1 row in set (0.00 sec)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27000195/viewspace-1724674/,如需转载,请注明出处,否则将追究法律责任。