选择列文本包含数组值的行

发布于 2024-10-27 00:15:06 字数 493 浏览 5 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

五里雾 2024-11-03 00:15:06

如果搜索数组是恒定的,或者很少变化,我建议使用另外两个表,“tags”和“tags-text”。

例如,示例中 id 2 的行包含 Fish,因为 Fish 在我们的“tags”表中,新记录将被放置在“tags-text”表中。当您使用数组进行搜索时,您可以搜索数组组件之一是否在“tags-text”表中,然后加入“text”表并返回文本和 id 并执行您需要的任何操作。

其他表的结构:

“标签”表

id    | tags
-------------------------------------- 
1     | fish
2     | dogs
3     | cats

“标签文本”表

text-id  | tags-id
-------------------------------------- 
2        | 1

这有帮助/有意义吗

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

id    | tags
-------------------------------------- 
1     | fish
2     | dogs
3     | cats

'tags-text' table

text-id  | tags-id
-------------------------------------- 
2        | 1

Does this help/make sense

金橙橙 2024-11-03 00:15:06

好吧,我想我已经找到了最简单的解决方案:让 PHP 创建 mysql 查询并使用 WHERE LIKE 解决它。

$search_array = array('fish','dogs','cat','panda');

$string = '';

foreach($search_array as $term) {
    $string = $string."text LIKE '%".$term."%' AND ";
}

foreach 循环的结果是:

"text LIKE '%fish%' AND LIKE '%dogs%' AND LIKE '%cat%' AND LIKE '%panda%' AND "

现在让我们删除该字符串的尾部并编写查询:

$string = substr($string, 0, -5); // removing " AND " at the end of the string

$query = "SELECT * FROM table WHERE $string";
$results = mysql_query($query);

无论如何,感谢其他答案:)

Ok I think I've found the easiest solution: let PHP create the mysql query and solve it with WHERE LIKE.

$search_array = array('fish','dogs','cat','panda');

$string = '';

foreach($search_array as $term) {
    $string = $string."text LIKE '%".$term."%' AND ";
}

The result of the foreach loop is:

"text LIKE '%fish%' AND LIKE '%dogs%' AND LIKE '%cat%' AND LIKE '%panda%' AND "

Now lets remove the tail of that string and write the query:

$string = substr($string, 0, -5); // removing " AND " at the end of the string

$query = "SELECT * FROM table WHERE $string";
$results = mysql_query($query);

Thanks for the other answers anyway :)

风为裳 2024-11-03 00:15:06

好吧,也许你应该尝试混合一下 mysql 和 php。
这是伪代码

select 100-1000 rows at one time from db
   use strpos to check each element in your array against the text column
       if element found 
           store it 
           if 2 elements found break the loop
       else 
           continue 

Ok, maybe you should try mixing mysql and php a bit.
Here is the pseudo-code

select 100-1000 rows at one time from db
   use strpos to check each element in your array against the text column
       if element found 
           store it 
           if 2 elements found break the loop
       else 
           continue 
无声静候 2024-11-03 00:15:06

像这样的事情也许...

$search_term = implode(",",$search_array);
SELECT * FROM your_table WHERE text IN ($search_term)";    

Something like this maybe ...

$search_term = implode(",",$search_array);
SELECT * FROM your_table WHERE text IN ($search_term)";    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文