简体   繁体   English

Codeigniter Active Records LIKE查询

[英]Codeigniter Active Records LIKE Query

I am making a news website with Codeigniter, and I have an Articles MySQL table like ID,Title,Body,Categories,Created etc... 我正在使用Codeigniter创建一个新闻网站,我有一个文章MySQL表,如ID,标题,正文,类别,创建等...

In Categories field I have category separated with comma(,) like... 在“类别”字段中,我将类别用逗号(,)分隔,如...

  • Article 1 Categories : National,Crime,Cinema 第1条分类:国家,犯罪,电影
  • Article 2 Categories : National,City,Drama 第2条分类:国家,城市,戏剧
  • Article 3 Categories : Funny,International,Cinema 第3条分类:搞笑,国际,电影
  • Article 4 Categories : National,Crime,Cinema 第4条分类:国家,犯罪,电影

I want to fetch article with Specific Category.. like National (1,2,4) . 我想获取特定类别的文章..像国家(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方法查询“ Categories字段

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 . 您的方法有许多缺点,从长远来看,将表格关系更改为Categories将更具可扩展性。 You can use a manytomany relationship and a join table to more easily query your categories. 您可以使用manytomany关系和连接表来更轻松地查询您的类别。

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. FIND_IN_SET将执行全表扫描,并且使用此逗号分隔方式将非常难以聚合,并获得文章/类别计数。


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. 比尔卡文将这种反模式作为其优秀着作中的第一章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM