2013-03-25 95 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

相關問題