views:

38

answers:

3

Hi,

i have a string table in my mysql/php table.

 id | str  

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

if i search with LIKE keyword for example "1" the result is below and its wrong

 id | str  

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

But the expected result is

 id | str  
 5  |  3,1,16

since "1" is only in the above row but unfortunately no idea,

help me with this,

Nithish.

+1  A: 

use REGEXP instead of LIKE http://dev.mysql.com/doc/refman/5.1/en/regexp.html

str REGEXP '1[,$]'

Michael Pakhantsov
This will match 21
M42
thnxs i cant vote up right now will do when i reach 15 of my score :)
Nithish
+1  A: 

you should use REGEXP instead of LIKE
select * from mytable WHERE str REGEXP '(^|,)1(,|$)'
^ stands for "beginning of the string" and $ for "end of the string"
see http://dev.mysql.com/doc/refman/5.1/en/regexp.html for more details

PierrOz
thnxs i cant vote up right now will do when i reach 15 of my score :)
Nithish
now you are 15 :)
PierrOz
@pierrOz wow you doing magic dude hope u got ma vote
Nithish
+4  A: 

what you're looking for is FIND_IN_SET function

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

better yet, consider normalizing your database

stereofrog
thats great geek frog you are thnxs :)
Nithish
The final query SELECT * FROM `tablename` WHERE FIND_IN_SET( '1', string) >0
Nithish