你有没有想过将源代码和标签分离到不同的表中?
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