Talha Aslam Talha Aslam - 4 months ago 13
PHP Question

MYSQL Like query only the keyword matchcase

We have designed a database but there was a bug in design and we are in big issue now, may be like query in any way help us out.

In my locations table of my database, I have some record's like this:

E2
W3,SE2,SW13,E2
SE2
W3,E2
W3,SE2


Now I want to fetch all the results that contains
E2
. If I use
like
it also gets
SE2
, and I don't want that.

Basically, I want to find
E2
even if it's in a row that has other values separated by
,
.

If i search for
SE2
then I don't want to get
E2
as result, even if it's in a row that has other values separated by
,
.

Basically, I want to find
SE2
even if it's in a row that has other values separated by
,
.

Answer

For records stored as comma-separated values, you're better off using FIND_IN_SET:

SELECT * FROM your_table WHERE FIND_IN_SET('E2', your_column) > 0;