user3176519
user3176519

Reputation: 403

php, mysql single query for multipe columns distinct values

Is it possible to retrieve records using single query in case like:

id  title         tags
1   First         yellow,blue
2   Second        green, yellow,red,
3   Third         black,purple

What I would like to do is select all records where keyword yellow appears. The result should return two records "First and Second"

Upvotes: 2

Views: 53

Answers (1)

Girish
Girish

Reputation: 12127

Better to use REGEX to get exact search

SELECT * FROM tblname WHERE 
    tags REGEXP '[[<:]]yellow[[:>]]'

Or you can also use FIND_IN_SET() function

SELECT * FROM tblname WHERE 
    FIND_IN_SET('yellow', tages) > 0

NOTE: FIND_IN_SET() function won't work correctly if tags not symmetric comma separated, if tags have white space between , then it would create problem

Upvotes: 2

Related Questions