MySQL query returns 0 rows when searching for value with dot (.) in string

为君一笑 提交于 2019-12-08 12:49:33

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?

Alex Bender

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.

sourav

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

 SELECT * FROM test WHERE `username` LIKE '%.%';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!