Question

I have in the database articles table :

id- cat  -  title 

1-  10,2 -  the title

2-  5,10 -  the title

3-  7    -  the title

4-  7,4,3 - the title

I want to select the articles where cat = '10' which means article id 1 and 2.

I have used MySQL LIKE but it does not work well.

Any idea ?

Was it helpful?

Solution

Use FIND_IN_SET()

select * from your_table
where find_in_set(10, cat) > 0

But actually the better solution would be to normalize your table and store only one value in a column!

You can do this by adding another table

article_categories table
------------------------
article_id
category_id
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top