php, mysql single query for multipe columns distinct values

75

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"

163

Answer

Solution:

Better to useREGEX to get exact search

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

Or you can also useFIND_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, iftags have white space between, then it would create problem

People are also looking for solutions to the problem: mysql - Why isn't my PHP Sign-Up not working?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.