Ella Sharakanski
Ella Sharakanski

Reputation: 2773

Escape field in LIKE query

The following query:

SELECT `att`.`val` FROM `att` WHERE NOT (`att`.`val` LIKE `att`.`val`);

Should return nothing, right?

But it returns all the vals that have a backslash in them.

How do I make it return nothing?

val is a varchar(1024) field (not primary and not null).

This of course is a useless query but it is a simplified version of another query that doesn't work because of the same reason.

I'm using mysql 5.6.20.

Thanks!

EDIT:

I want to escape every character that needs escaping so the query will work as intended. Not just backslashes.

Upvotes: 3

Views: 110

Answers (1)

user359040
user359040

Reputation:

Try changing the condition to:

WHERE NOT (`att`.`val` LIKE replace(`att`.`val`,'\\','\\\\'))

- this replaces single backslashes with double backslashes in val, so that instead of acting as escape characters, they are treated as literal backslashes by the like expression. (The first backslash escapes the second.)

Upvotes: 2

Related Questions