Talha Aslam
Talha Aslam

Reputation: 101

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 ,.

Upvotes: 2

Views: 41

Answers (2)

rjdown
rjdown

Reputation: 9227

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;

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133360

You can try with some or condition

select * from my_table 
where my_column = 'E2' 
or my_column like '%,E2'
or my_column like 'E2,%' 

for SE2 the query is

select * from my_table 
where my_column = 'SE2' 
or my_column like '%,SE2'
or my_column like 'SE2,%' 
or my_column like '%,SE2,%'

or generally

select * from my_table 
where my_column = 'your_key' 
or my_column like '%,your_key'
or my_column like 'your_key,%' 
or my_column like '%,your_key,%'

Upvotes: 0

Related Questions