2013-03-25 100 views
1

我有记录,如:FIND_IN_SET()在我的数据库没有找到的所有值

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 

但“智能手机”的值也是第三排,所以应该要回到第1和第3行。

我哪里去错了?或者是否有任何不同的搜索方法,而不是find_in_set()

回答

0

find_in_set()正确的语法是:

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

编辑:

您可以使用诸如文本搜索:

SELECT * 
FROM `tablename` 
WHERE tags LIKE '%TAG%' 
+0

我试过这个:'SELECT * FROM WHERE FIND_IN_SET(“ta​​gs”,“smartphone”);'但是它没有返回任何东西 – Sky 2013-03-25 08:33:34

+0

是的,它不像(%string%')或者它没有搜索字符串的中间 – 2013-03-25 08:39:07

+1

这个答案有误导性。示例中的find_in_set用法是正确的。引起问题的逗号之前和/或之后的空格。你可以从你的数据库或查询中删除空格(虽然不推荐):'FIND_IN_SET('smartphone',REPLACE(tags,',',','))' – 2015-01-02 15:19:13

0

如果在逗号后有空间,那么FIND_IN_SET韩元工作不正常。

它将在smartphone, apple, apps, touchscreen中找到,但它不会在apple, smartphone, apps, touchscreen中找到。

如需其他解决方案,请尝试mysql full text search

相关问题