Thilak
Thilak

Reputation: 590

Need help for mysql query

In my MySQL table I have a column named member_id. That column stores values like this:

1,2,3,4,5

I need to check that by using

SELECT * 
FROM member 
WHERE 5 IN member_id

It's not working well. Please help me write a SQL query that will find the appropriate results.

Upvotes: 1

Views: 88

Answers (3)

Gratzy
Gratzy

Reputation: 9389

SELECT * FROM member WHERE member_id LIKE '%5%'

Upvotes: 0

Quassnoi
Quassnoi

Reputation: 425823

Use this query:

SELECT  *
FROM    mytable
WHERE   FIND_IN_SET(5, member_id)

Upvotes: 7

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171579

Quassnoi's method is preferred for MySQL, but for a portable cross-platform technique, you can do this:

select * 
from member 
where member_id = '5' 
    or member_id like '5,%' 
    or member_id like '%,5,%' 
    or member_id like '%,5' 

This assumes there is no extra whitespace in your data.

Upvotes: 1

Related Questions