简体   繁体   中英

mysql-How to handle query search with \(backslash)?

i have inserted a column in mysql table with backslash(\\) like user\\12345 .But when i see in the table values i am not seeing the forward slash(\\) . I am just seeing user12345 .

When i query the just inserted row with backslash(\\) with the below possible ways, i am always getting the zero records.

select * from users where user='user12345'
select * from users where user='user\12345'
select * from users where user='user\\12345'

when i copy paste the column value into notepad+ i could see there is a carriage retun in the end of every column values. user12345CRLF, as of now i can't update/remove the carriage return. but i want to fetch the records with carriage return. how can i do this?

I don't want to use like query, and i want to query for the exact username. how can i do this?

Backslash character \\ is used for escape sequence. when you insert value like 'user\\12345' then by default mysql server takes it as '\\1' as escape character but it is not any special character so it discard the \\ from the string.

Hitesh> update test set fname='user\344';
Query OK, 2 rows affected (0.06 sec)
Rows matched: 2  Changed: 2  Warnings: 0
Hitesh> select fname from test;
+---------+
| fname   |
+---------+
| user344 |
| user344 |
+---------+
2 rows in set (0.00 sec)

If you want to store \\ in string then you should use \\

Hitesh> update test set fname='user\\344';
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2  Changed: 2  Warnings: 0

Hitesh> select fname from test;
+----------+
| fname    |
+----------+
| user\344 |
| user\344 |
+----------+
2 rows in set (0.00 sec)

if you don't want to set \\ as escape character then you can enable the sql mode NO_BACKSLASH_ESCAPES.

Hitesh> SET sql_mode = 'NO_BACKSLASH_ESCAPES';
Query OK, 0 rows affected (0.00 sec)

Hitesh> update test set fname='user\344';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2  Changed: 0  Warnings: 0

Hitesh> select fname from test;
+----------+
| fname    |
+----------+
| user\344 |
| user\344 |
+----------+
2 rows in set (0.00 sec)

for case of carriage return to end of column, in query you can find the string which ends with \\r in where clause and select column you can skip the last character which is \\r

 Hitesh> set @@sql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
 Query OK, 0 rows affected (0.00 sec)

 Hitesh> update test set fname='user344\r';
 Query OK, 2 rows affected (0.17 sec)
 Rows matched: 2  Changed: 2  Warnings: 0  

 Hitesh> select left( fname, CHAR_LENGTH(fname)-1) as fname from test where fname like '%\r';
    +---------+
    | fname   |
    +---------+
    | user344 |
    | user344 |
    +---------+
    2 rows in set (0.00 sec)

Thanks a ton @Hitesh,@Barmar again. I figured it out the query for exact match search. select * from users where user='user12345\\r'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM