I am making a news website with Codeigniter, and I have an Articles MySQL table like ID,Title,Body,Categories,Created etc...
In Categories field I have category separated with comma(,) like...
I want to fetch article with Specific Category.. like National (1,2,4). I tried many methods but nothing seems to work.
Please Help Thanks.
you can use FIND_IN_SET
method to query your Categories
field
FIND_IN_SET('Crime', your_table.Categories)
Your approach has a number of shortcomings, It would def be more scalable in the long run to change your tables relationship to Categories
. You can use a manytomany relationship and a join table to more easily query your categories.
FIND_IN_SET will do a full table scan, and using this comma seperated way will be very difficult to aggregate, and get article/category counts.
Is storing a delimited list in a database column really that bad?
Bill Karwin has included this anti pattern as the first chapter in his excellent book.