Question

I want to do a simple SELECTand LIKE statements in MySql 5.5.30. The data i'm looking for is a text presented like this in the database : Test d\'un article.

I searched how to escape \' and found that i must escape each character. I do the following:

SELECT
    description
FROM
    myTable
WHERE
    description
LIKE
    '%\\\'%'

This query works just fine and returns the data i want. But if I look for the entire text like this :

LIKE
    'Test d\\\'un article'

The query returns no result. I found out that the problem is from the d before the backslashes, as the following query works too

LIKE
    '%\\\'un article'

and this one don't

LIKE
    '%d\\\'%'

I couldn't find any similar problem elsewhere and hope you can help me with this.

PS. : The table charset is latin1_swedich_ci and the engine is MyISAM

Was it helpful?

Solution

The problem is that in LIKE, you've to double escape the backslashes. So following query will work

SELECT
    description
FROM
    myTable
WHERE
    description
LIKE
    '%d\\\\\'un article%'

in '%d\\\\\'un article%', first \\\\ is double escaping of \, and \' is escaping of '

related SO question:
Why I need to double-escape (use 4 \) to find a backslash ( \ ) in pure SQL?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top