用于改变where条件的Sql
我正在使用 codeigniter 开发一个食谱网站(我使用 mysql 作为数据库)。用户可以输入 任意数量的成分。我需要搜索包含这些成分的食谱。即,如果用户放入鸡蛋和西红柿,我需要搜索使用两者的食谱。食谱搜索结果应按所需额外成分的数量较少进行排序。由于成分数量不同,sql查询怎么写?如何对搜索结果进行排序? 知道如何去做吗?先感谢您。
I am developing a recipe site using codeigniter (i am using mysql as database). User can enter
any number of ingredients.I need to search recipes which contain these ingredients. i.e if user puts eggs and tomatoes i need to search recipes which uses both.recipe Search result should be order by less number of extra ingredient required. Since the number of ingredient varies how to write sql query? How to order search result?
Any idea of how to go about it? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的解决方案(恕我直言,不是最好的,而是最简单的解决方案之一)是利用 MySQL 的全文索引。您可以首先阅读 MySQL 关于全文索引的文档,它对于制作一个小型搜索引擎非常有用。借用维基百科的解释:
因此,在我的建议中,您可以使用与此类似的表:
注意事项:
仅将全文索引添加到
ingredients
字段。将成分存储在
ingredients
字段中,用空格分隔,不要使用逗号。将配料存储在两个地方,上面的
ingredients
字段仅用于搜索。您可能最好使用另一个表来存储其他用例的成分列表。由于您使用的是全文索引,因此可以使用
BOOLEAN MODE
语法。如果您想搜索包含egg
和tomato
的食谱,请使用+eggs +tomato
,如果您想搜索包含tomato 的食谱,请使用
但不是+eggs +tomato
>eggtomato
,请使用+egg -tomato
。详细解释参见MySQL手册。再说一遍,这可能不是最好的解决方案,但这是我能想到的第一件事,而且当然很简单。
一些注意事项:
默认情况下,MySQL 的全文引擎不识别少于 4 个字符的单词,您可以更改此设置。
如果不使用布尔模式,如果结果超过数据集的 50%,MySQL 全文搜索将返回空。
全文搜索将忽略停用词,列表如下.
The simplest solution (not the best, IMHO, but one of the easiest) is to utilize MySQL's fulltext index. You can start by reading MySQL's documentation on fulltext index, it's tremendously useful in making a little search engine. Borrowing explanation from wikipedia:
So, in my suggestion, you can have this table similar to this:
Considerations:
Add fulltext index to
ingredients
field only.Store ingredients inside the field
ingredients
, separated with space, don't use comma.Store ingredients in two places, the
ingredients
field above is just for searching. You might be better off with another table to store ingredient list for other use cases.Since you're using fulltext index, you can use
BOOLEAN MODE
syntax. If you want to search for recipes that hasegg
andtomato
use+eggs +tomato
, if you want to search for recipes that containsegg
but nottomato
, use+egg -tomato
. See MySQL's manual for detailed explanation.Again, it might not be the best solution, but it's the first thing that I can think of, and it's certainly easy.
A couple of caveats:
MySQL's fulltext engine doesn't recognize words with lower than 4 characters by default, you can change this.
If you don't use boolean mode, MySQL fulltext search will return empty if the result if more than 50% of your dataset.
Fulltext search will ignore stop words, here's the list.
http://www.jarrodgoddard.com/web-development /advanced-web-site-search-with-sql 这是一篇有关如何使用 MySQL 查询获取基于相关性的搜索结果的文章。只要发挥一点创造力,您就可以通过按某种成分的数量订购它们来添加相关性。
配料如何保存?在我希望的关系表中(recipe_id、compute_id、amount)?这使得搜索更容易。
http://www.jarrodgoddard.com/web-development/advanced-web-site-search-with-sql here's an article on how to use MySQL Queries to get relevance-based search results. With a little bit of creativity you can add relevance yourself by ordering them by the amount of a certain ingredient.
How are ingredients saved? In a relational table I hope (recipe_id, ingredient_id, amount)? That makes it easier to search.