2

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"

2
  • Try select * from table where tags like '%yellow%' Commented Jan 17, 2015 at 3:58
  • why isn't a proper 1-N structure used here? Commented Jan 17, 2015 at 4:06

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

FIND_IN_SET() works perfectly. All my tags are imploded before being stored.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.