Reputation: 451
My SQL query is:
SELECT * FROM listings WHERE title LIKE '%Debbie\'s Pet Grooming%'
However, when I run this query I get no results. Can you please suggest why. My database table is below:
Upvotes: 6
Views: 821
Reputation: 147166
Your problem is that somehow you have managed to get an actual backslash in the value in the database, so you need to search for that. Try this:
SELECT * FROM listings WHERE title LIKE '%Debbie\\\\\'s Pet Grooming%'
From the manual:
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
Upvotes: 3
Reputation: 133370
You should use
WHERE title LIKE '%Debbie\\\'s Pet Grooming%' ;
.
SELECT * FROM (
select "Debbie\'s Pet Grooming" title
) t
WHERE title LIKE '%Debbie\\\'s Pet Grooming%' ;
but be user you have not space between Debbie and '
The first \ is for escape the second and the third for the single quote
Upvotes: 1