你有沒有想過將源代碼和標籤分離到不同的表中?
Source Table
ID, Title, Source
1 "t1" "sc"
2 "t2" "sc"
3 "t3" "sc"
Tag Table
ID, Tag
1 "Zend"
2 "Smarty"
3 "jquery"
4 "doctrine"
SourceTagLink Table
SourceID, TagID
1 1
1 2
2 1
2 3
3 4
你有,你可以選擇,或添加標籤唯一列表的方式。 你不會做任何字符串解析,所以你的選擇會更快。與您在本網站上爲您的帖子分配標籤的方式類似。
編輯
這是我用一個多值的字符串轉換成表格與它寫一個列的函數MSSQL,但你應該能夠將其轉換到MySQL
CREATE FUNCTION [dbo].[ParseString](@String NVARCHAR(4000), @Delimiter CHAR(1)=',')
RETURNS @Result TABLE(tokens NVARCHAR(4000))
AS
BEGIN
-- We will be seearching for the index of each occurrence of the given
-- delimiter in the string provided, and will be extracting the characters
-- between them as tokens.
DECLARE @delimiterIndex INT
DECLARE @token NVARCHAR(4000)
-- Try to find the first delimiter, and continue until no more can be found.
SET @delimiterIndex = CHARINDEX(@Delimiter, @String)
WHILE (@delimiterIndex > 0)
BEGIN
-- We have found a delimiter, so extract the text to the left of it
-- as a token, and insert it into the resulting table.
SET @token = LEFT(@String, @delimiterIndex-1)
INSERT INTO @Result(tokens) VALUES (LTRIM(RTRIM(@token)))
-- Chop the extracted token and this delimiter from our search string,
-- and look for the next delimiter.
SET @String = RIGHT(@String, LEN(@String)[email protected])
SET @delimiterIndex = CHARINDEX(@Delimiter, @String)
END
-- We have no more delimiters, so place the remainder of the string
-- into the result as our last token.
SET @token = @String
INSERT INTO @Result(tokens) VALUES (LTRIM(RTRIM(@token)))
RETURN
END
基本上你怎麼稱呼它像
ParseString('this be a test', ' ')
it will return a single column table
this
be
a
test
ParseString('this:be a test', ':')
returns
this
be a test
你可以在更新觸發器添加一個調用該函數來填充新表,以幫助您做出選擇更加容易。一旦觸發建成,只是做類似下面
Update yourTable
Set Title = Title
,填補火扳機和填充新表,讓你的一切更容易在不影響現有代碼的簡單更新。當然,你需要用一個單一的替換所有已知的定界符來使其工作。
添加或修改的任何新記錄都會自動激發觸發器。
當標記爲負時做任何一條評論....這個問題有什麼問題嗎? – Developer 2011-03-07 11:35:26