选择列文本包含数组值的行
我正在 php/mysql 中构建一个搜索函数,并且正在寻找正确的 MySql 函数。我的表看起来像这样:
id | text
--------------------------------------
1 | I like pony's.
2 | Do you like fish?
3 | We like fishes!
我想在“文本”列中搜索数组的精确值之一,例如:
$search_array = array('fish','dogs','cat','panda');
我正在寻找正确的 MySql 函数以仅返回第二行(使用当前数组) )。该数组可以包含数百个值。
我有 6000 多行,每天都以 +/- 400 的速度增长。我尝试过 REGEXP,但对于大型数组,大约需要 10 秒才能返回相应的行。
请帮忙,我已经为此奋斗了将近 3 天......提前致谢!
I'm building a search function in php/mysql and I'm looking for the right MySql function. My table sort of looks like this:
id | text
--------------------------------------
1 | I like pony's.
2 | Do you like fish?
3 | We like fishes!
I want to search the column 'text' for one of the exact values of an array, for example:
$search_array = array('fish','dogs','cat','panda');
I'm looking for the right MySql function to return only the second row (with the current array). The array can contain hundreds of values.
I have 6000+ rows, growing everyday with +/- 400. I've tried REGEXP but with a large array, it took about 10 seconds before it returned the corresponding rows.
Please help, I'm fighting with this for almost 3 full days now... Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果搜索数组是恒定的,或者很少变化,我建议使用另外两个表,“tags”和“tags-text”。
例如,示例中 id 2 的行包含 Fish,因为 Fish 在我们的“tags”表中,新记录将被放置在“tags-text”表中。当您使用数组进行搜索时,您可以搜索数组组件之一是否在“tags-text”表中,然后加入“text”表并返回文本和 id 并执行您需要的任何操作。
其他表的结构:
“标签”表
“标签文本”表
这有帮助/有意义吗
If the search array is constant, or changes infrequently, I recommend having another two tables, 'tags' and 'tags-text'.
For example, the row with id 2 in your example contains fish, since fish is in our 'tags' table a new record will be placed in a 'tags-text' table. When you are searching with your array, you can search if one of the array components is in the 'tags-text' table, and join the 'text' table and return the text and id and do whatever you need.
Structure of other tables:
'tags' table
'tags-text' table
Does this help/make sense
好吧,我想我已经找到了最简单的解决方案:让 PHP 创建 mysql 查询并使用 WHERE LIKE 解决它。
foreach 循环的结果是:
现在让我们删除该字符串的尾部并编写查询:
无论如何,感谢其他答案:)
Ok I think I've found the easiest solution: let PHP create the mysql query and solve it with WHERE LIKE.
The result of the foreach loop is:
Now lets remove the tail of that string and write the query:
Thanks for the other answers anyway :)
好吧,也许你应该尝试混合一下 mysql 和 php。
这是伪代码
Ok, maybe you should try mixing mysql and php a bit.
Here is the pseudo-code
像这样的事情也许...
Something like this maybe ...