1

我的数据库中有以下记录:

hashtag -   description -   tags -  location -  time -  day

#social -  description - social media, facebook, social networking sites, social media, smartphone - usa - Tuesday

#php -  description - programming language, coding, open source - usa - Tuesday

#iphone -  description - smartphone, apple, iphone apps, costly phone, touch screen - usa - Tuesday

这里的标签是逗号分隔的值。

现在,如果用户搜索programming- 结果将是2nd row.

"coding"  - 2nd row
"smartphone" - 3rd & 1st row.
"iphone" - 3rd row

所以,这就像搜索标签或直接哈希关键字。第一列是hashkeyword。标签以逗号分隔存储在数据库中。

我正在使用find_in_set()它,但它似乎无法正常工作。

SELECT * FROM `hash`WHERE `hashtag` LIKE 'smartphone' OR find_in_set('smartphone', tags)

它只返回第一行,即

iphone -    iphone description - smartphone, apple, apps, touchscreen - India   - tuesday

但是“智能手机”值也在第三行,所以它应该返回第一行和第三行。

我哪里错了?或者有什么不同的方法可以像这样搜索而不是find_in_set()

4

2 回答 2

1

如果逗号后有空格,则FIND_IN_SET无法正常工作。

它会找到 insmartphone, apple, apps, touchscreen但它不会找到 in apple, smartphone, apps, touchscreen

对于替代解决方案,请尝试mysql full text search

于 2013-03-25T08:42:00.247 回答
0

find_in_set()的正确语法是:

SELECT *
FROM `tablename`
WHERE FIND_IN_SET( `column`, 'val1,val2' )

编辑:

您可以使用 LIKE 进行文本搜索:

SELECT *
FROM `tablename`
WHERE tags LIKE '%TAG%'
于 2013-03-25T08:31:20.137 回答