2010-08-30 58 views
2

我在我的mysql/php表中有一个字符串表。mysql字符串搜索算法查询帮助

id | str 

3 | 2,4,5,6 
4 | 7,8,9,14 
5 | 3,1,16 

如果我LIKE关键字,例如搜索 “1” 的结果低于其错误

id | str 

4 | 7,8,9,14 
5 | 3,1,16 

但该预期的结果

id | str 
5 | 3,1,16 

,因为 “1”只是在上面一排,但不幸没有想法,

帮我机智h这,

Nithish。

回答

4

‘字符串的结束’您要查找的是‘字符串的开始’ 10功能

select find_in_set('1', '3,2,1'); => 3 
    select find_in_set('1', '3,2,14'); => 0 

更好,但考虑normalizing数据库

+0

多数民众赞成在极大的怪胎青蛙你是thnxs :) – Nithish 2010-08-30 08:56:45

+1

最后的查询 SELECT * FROM'tablename' WHERE FIND_IN_SET('1',string)> 0 – Nithish 2010-08-30 08:58:15

1

你应该使用,而不是LIKE
REGEXP SELECT * FROM MYTABLE其中str REGEXP“(^ |)1 (,| $)”
^代表和$为
看到http://dev.mysql.com/doc/refman/5.1/en/regexp.html更多细节

+0

thnxs 15我现在不能投票了当我达到15我的分数:) – Nithish 2010-08-30 08:55:40

+0

现在你是15 :) – PierrOz 2010-08-30 09:11:44

+0

哇你做魔法的家伙希望你得到马投票 – Nithish 2010-08-30 09:14:40