zq29 Posted February 28, 2007 Share Posted February 28, 2007 One of the fields in a MySQL table contains a comma delimted string of keywords, I'm trying to return rows where the terms in my query match any number of keywords in that string. What I started out with was: SELECT * FROM `table` WHERE `field` REGEXP ',?keyword,?' Which doesnt really work as it matches combinations of characters within the whole string so would match 'cat' inside 'concatination' as one of the keywords in the string. I guess what I need is something like this so that it matches keywords surrounded by either a comma, and/or the start/end of the string: SELECT * FROM `table` WHERE `field` REGEXP '(^|,)?keyword(,|$)' But this expression doesn't seem to work. Any ideas? Link to comment https://forums.phpfreaks.com/topic/40520-solved-matching-keywords-within-a-comma-delimited-string/ Share on other sites More sharing options...
zq29 Posted February 28, 2007 Author Share Posted February 28, 2007 Think I figured it out: SELECT * FROM `table` WHERE `field` REGEXP '^(\w,)*keyword(,\w)*$' Does that look right? Link to comment https://forums.phpfreaks.com/topic/40520-solved-matching-keywords-within-a-comma-delimited-string/#findComment-196125 Share on other sites More sharing options...
effigy Posted February 28, 2007 Share Posted February 28, 2007 This should work; it's the same as you posted with the "?" removed: SELECT * FROM `table` WHERE `field` REGEXP '(^|,)keyword(,|$)' Link to comment https://forums.phpfreaks.com/topic/40520-solved-matching-keywords-within-a-comma-delimited-string/#findComment-196167 Share on other sites More sharing options...
zq29 Posted February 28, 2007 Author Share Posted February 28, 2007 This should work; it's the same as you posted with the "?" removed: SELECT * FROM `table` WHERE `field` REGEXP '(^|,)keyword(,|$)' Ah hah, thanks Effigy Was still having troubles with my "think-I've-fixed-it" suggestion, until I realised \w isn't a word boundary in MySQLs RegExp engine. Your suggestion appears to be working brilliantly. Ta. Link to comment https://forums.phpfreaks.com/topic/40520-solved-matching-keywords-within-a-comma-delimited-string/#findComment-196173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.