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