当在字符串中搜索带点 (.) 的值时,MySQL 查询返回 0 行

发布于 2024-12-20 04:03:18 字数 434 浏览 2 评论 0原文

如果我尝试在 mysql 数据库中搜索一个值,并且该字符串值中包含点,则查询将返回 0 行。示例:

SELECT * FROM table WHERE `username`='marco.polo'  --> 0 rows
SELECT * FROM table WHERE `username` LIKE '%.polo%'  --> 0 rows
SELECT * FROM table WHERE `username` LIKE 'polo'  --> Success

将服务器和数据库移至其他位置后出现此情况。我知道点是一组扩展正则表达式,但它不应该应用于 equal 或 LIKE 运算符,只是因为我在查询中不使用 REGEXP。

我已经在本地数据库上测试了相同的查询,效果很好。 mysql 中是否有一个特殊的设置以不同于通常的方式处理点?

If I try to search for a value in mysql database and the string value contains dot in it, query returns 0 rows. Example:

SELECT * FROM table WHERE `username`='marco.polo'  --> 0 rows
SELECT * FROM table WHERE `username` LIKE '%.polo%'  --> 0 rows
SELECT * FROM table WHERE `username` LIKE 'polo'  --> Success

This appeared after moving server and database to another place. I know that dot is a set of extended regular expressions, but it should not apply to equal nor LIKE operator, simply because I don't use REGEXP in query.

I've tested the same query on my local database and it works fine.
Could there be a special setting in mysql that treats dot differently than it usually does?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╄→承喏 2024-12-27 04:03:19

user1084605,我尝试复制该问题(使用MySQL版本5.1.37),但得到与您完全相反的结果。请参阅下文:

mysql> create table test (username varchar(100));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test values ('marco.polo');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM test WHERE `username`='marco.polo';
+------------+
| username   |
+------------+
| marco.polo | 
+------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM test WHERE `username` LIKE '%.polo%';
+------------+
| username   |
+------------+
| marco.polo | 
+------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM test WHERE `username` LIKE 'polo';
Empty set (0.00 sec)

根据 MySQL 文档,使用 LIKE 运算符时唯一的特殊字符是“%”(百分比:匹配 0、1 或多个字符)和“_”(下划线:匹配一个且仅一个字符)。
http://dev.mysql.com/doc/refman /5.0/en/string-comparison-functions.html

一个“。” (句点) 对于 MySQL 的 REGEXP 运算符确实具有特殊含义,但它仍应与列中的字面句点匹配。
http://dev.mysql.com/doc/refman/5.0/en /regexp.html

你能复制我上面运行的 SQL 语句并将你的结果粘贴到回复中吗?

user1084605, I tried to replicate the problem (using MySQL version 5.1.37), but got exactly the opposite results as you. See below:

mysql> create table test (username varchar(100));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test values ('marco.polo');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM test WHERE `username`='marco.polo';
+------------+
| username   |
+------------+
| marco.polo | 
+------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM test WHERE `username` LIKE '%.polo%';
+------------+
| username   |
+------------+
| marco.polo | 
+------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM test WHERE `username` LIKE 'polo';
Empty set (0.00 sec)

According to the MySQL docs, the only special characters when using the LIKE operator are "%" (percent: matches 0, 1, or many characters) and "_" (underscore: matches one and only one character).
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html

A "." (period) does have special meaning for MySQL's REGEXP operator, but it should still match a literal period in your column.
http://dev.mysql.com/doc/refman/5.0/en/regexp.html

Can you replicate the SQL statements I ran above and paste your results in reply?

孤独难免 2024-12-27 04:03:19

下面的查询处理列中只有 DOT 运算符时的情况。

 SELECT * FROM test WHERE `username` LIKE '%.%';

The below query takes care of the scenario when we have only DOT operator in the columns.

 SELECT * FROM test WHERE `username` LIKE '%.%';
玻璃人 2024-12-27 04:03:19

正如 @cen 已经提到的,字符集可能会导致该问题。
我有这个示例:

`email` VARCHAR(45) CHARACTER SET 'armscii8' NOT NULL,

这是我收到的 .sql 转储中的。

所以,当我尝试用这封电子邮件获取对象时
我无法得到它。

As @cen already mentioned, character set can causes that problem.
I have had this sample:

`email` VARCHAR(45) CHARACTER SET 'armscii8' NOT NULL,

this is was in the .sql dump, which I receive.

So, when I was trying to fetch object with this email
I couldn't get it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文