MySQL 查询中替换单引号?


若要置换单引号,可以使用 replace()。以下为其语法 −

update yourTableName set yourColumnName=replace(yourColumnName,'\'','');

我们首先创建一个表 −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Sentence varchar(100)
   );
Query OK, 0 rows affected (0.17 sec)

使用 insert 命令在表中插入一些记录。为该句子添加单引号 −

mysql> insert into DemoTable(Sentence) values('Chris\'s Father');
Query OK, 1 row affected (0.07 sec)

mysql> insert into DemoTable(Sentence) values('Larry\'s Mother');
Query OK, 1 row affected (0.06 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

这将生成以下输出 −

+----+----------------+
| Id | Sentence       |
+----+----------------+
| 1  | Chris's Father |
| 2  | Larry's Mother |
+----+----------------+
2 rows in set (0.00 sec)

以下是用于替换单引号的查询 −

mysql> update DemoTable set Sentence=replace(Sentence,'\'','');
Query OK, 2 rows affected (0.07 sec)
Rows matched: 2 Changed: 2 Warnings: 0

让我们在表中显示记录,以检查单引号是否已被替换。

mysql> select *from DemoTable;

这将生成以下输出 −

+----+---------------+
| Id | Sentence      |
+----+---------------+
| 1  | Chriss Father |
| 2  | Larrys Mother |
+----+---------------+
2 rows in set (0.00 sec)

更新日期:2019-07-30

497 次浏览

开启你的职业生涯

通过完成课程获得认证

立即开始
广告
© . All rights reserved.