gpt4 book ai didi

php - MySQL中如何根据关键字过滤数据?

转载 作者:行者123 更新时间:2023-11-29 04:35:06 43 4
gpt4 key购买 nike

我在检索有关过滤关键字的数据时遇到问题。我有这样的消息表

我的过滤表:-

+--+-------+-----------+---------+---------------+
|id|user_id|for_page_id|key_words| message |
+--+-------+-----------+---------+---------------+
| 1| 12| 2 |he,you |You are awesome|
| 2| 12| 2 |the,book |this is good |
+--+-------+-----------+---------+---------------+

我有一个关键词串,我想为那个关键词找到相应的消息
我试过这个:-

$string='he is good';
$keyWords=explode(' ',$string);
$query="SELECT * FROM `filter` WHERE `key_words` like '".$keyWords[0]."' or`key_words` like
'".$keyWords[1]."' or`key_words` like '".$keyWords[2]."'";
echo ($query);

查询看起来像

SELECT * FROM `filter` WHERE `key_words` like 'he' or`key_words` like 'is' or`key_words` like 'good'

输出为

| 1|     12|        2  |he,you   |You are awesome|
| 2| 12| 2 |the,book |this is good |

接受的输出是

| 1|     12|        2  |he,you   |You are awesome|

如有任何想法,我们将不胜感激。

最佳答案

FIND_IN_SET() 函数可能就是您正在寻找的。

SELECT FIND_IN_SET('b','a,b,c,d');
-> 2

如果在列表中找不到该字符串,则返回 0。

这是 MySQL documentation 的链接

关于php - MySQL中如何根据关键字过滤数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45072241/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com