MySQL 中的反斜杠 \\,真是太坑了!!
阅读本文大概需要 4.5 分钟。
来自:https://blog.csdn.net/qq_39390545/article/details/117296607
一、INSERT语句中有反斜杠(\)
1、实际测试
INSERT INTO `demo0526` (`id`, `text`) VALUES (null, 'D:\陈哈哈\加班');
INSERT INTO `demo0526` (`id`, `text`) VALUES (null, 'D:\\陈哈哈\\加班');
INSERT INTO `demo0526` (`id`, `text`) VALUES (null, 'D:\\\陈哈哈\\\加班');
INSERT INTO `demo0526` (`id`, `text`) VALUES (null, 'D:\\\\陈哈哈\\\\加班');
INSERT INTO `demo0526` (`id`, `text`) VALUES (null, 'D:\\\\\陈哈哈\\\\\加班');
mysql> select * from demo0526;
+----+-----------------------+
| id | text |
+----+-----------------------+
| 1 | D:陈哈哈加班 |
| 2 | D:\陈哈哈\加班 |
| 3 | D:\陈哈哈\加班 |
| 4 | D:\\陈哈哈\\加班 |
| 5 | D:\\陈哈哈\\加班 |
+----+-----------------------+
5 rows in set (0.00 sec)
2、是啥原理?
二、SELECT查询反斜杠(\)
1、实际测试
mysql> select * from demo0526;
+----+-----------------------+
| id | text |
+----+-----------------------+
| 1 | D:陈哈哈加班 |
| 2 | D:\陈哈哈\加班 |
| 3 | D:\陈哈哈\加班 |
| 4 | D:\\陈哈哈\\加班 |
| 5 | D:\\陈哈哈\\加班 |
+----+-----------------------+
mysql> SELECT * from demo0526 where text like '%\%';
Empty set (0.00 sec)
mysql> SELECT * from demo0526 where text like '%\\%';
Empty set (0.00 sec)
mysql> SELECT * from demo0526 where text like '%\\\\%';
+----+-----------------------+
| id | text |
+----+-----------------------+
| 2 | D:\陈哈哈\加班 |
| 3 | D:\陈哈哈\加班 |
| 4 | D:\\陈哈哈\\加班 |
| 5 | D:\\陈哈哈\\加班 |
+----+-----------------------+
4 rows in set (0.00 sec)
mysql> SELECT * from demo0526 where text like '%\\\\\\\\%';
+----+-----------------------+
| id | text |
+----+-----------------------+
| 4 | D:\\陈哈哈\\加班 |
| 5 | D:\\陈哈哈\\加班 |
+----+-----------------------+
2 rows in set (0.00 sec)
2、又是啥原理?
mysql> SELECT * from demo0526 where text = '\\\\';
+----+------+
| id | text |
+----+------+
| 7 | \\ |
+----+------+
1 row in set (0.00 sec)
总结
推荐阅读:
最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
朕已阅
评论