上我得出了一個結論,其他的答案閱讀您的意見之後,你想要的是讓臺來自表格的行,並且您希望每個集合在item_des列中具有相似的值。至少這就是你的例子。
問題出在您定義「相似」的方式。據我所知,你不想提供任何額外的數據查詢來定義你正在尋找什麼樣的相似性。 AshReva和Naryl假設你這麼做。這就是爲什麼他們建議你在查詢中使用LIKE '%flower%' or LIKE 'fruit'
。
我猜你在問什麼是如何得到名單('fruit', 'flower', ...)
,因爲你沒有一個。你想要一個能夠爲你找到一個的查詢。
這不是一件容易的事情,它需要您做出一些決策。這項任務相當複雜,所以我不打算提供一個隨時可用的解決方案。我將介紹幾個相當直接的步驟,你需要通過。
首先你需要標記你的item_des字段。你想要的是另一個名爲t的表,例如一個字段叫做token。標記你的例子後,你應該得到這樣一個表:
token
fruit
books
beautiful
flower
&
nice
smell
gud
fruit
flower
你可能必須編寫自己的標記函數。請點擊此處: Is there something analogous to a split() method in mySql?
然後刪除重複項(在列上執行distinct
)。所以,你得到: 令牌 水果 書 美麗 花 & 不錯 氣味 GUD
那麼你可能想以某種方式刪除無效的標記。您可以手動執行此操作。你可以對一些關鍵字或字典進行自動匹配。您可能會應用一些啓發式方法,例如刪除長度爲1個字符的標記。
之後,你只需做兩個表之間的匹配,你的原始表(假設它被稱爲input_data)和最終表t,其中包含您的令牌:
select item_name, item_des, token
from input_data, t
where item_des like concat('%',t.token,'%')
order by token
那麼你應該得到這樣的事情即:
item_name item_des token
jasmine beautiful flower & nice smell beautiful
jasmine beautiful flower & nice smell &
jasmine beautiful flower & nice smell nice
jasmine beautiful flower & nice smell smell
rose flower flower
jasmine beautiful flower & nice smell flower
orange gud fruit gud
orange gud fruit fruit
apple fruit fruit
books books books
我希望這是你所需要的。
請準確描述爲什麼茉莉花和玫瑰花在你的第一個例子中被選中 – Bohemian 2013-03-10 11:04:49