homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

Get column if it has a word

 10:31 pm on Jan 30, 2010 (gmt 0)

This should be pretty easy...I'm trying to select rows in which the `description` column has a certain word. I tried "...WHERE `description` LIKE '{$word}'" but I guess without any helper symbols (_ or %), LIKE is equivalent to '='. How else would I do this?

To clarify, I want to prevent any rows from being displayed if the query is looking for "bar" and the description has the word "barcode".




 2:05 am on Jan 31, 2010 (gmt 0)

You can use a regular expression search for word boundaries, but you'll probably want to combine that with a LIKE '%bar%" first to reduce the number of rows checked by the regular expression. Something like this (untested code off the top of my head):

FROM `table`
WHERE `description` LIKE '%bar%'
AND `description` REGEXP '[[:<:]]bar[[:>:]]';

This should match 'bar' without matching 'barcode'.

REGEXP Documentation: [dev.mysql.com...]


 7:54 pm on Jan 31, 2010 (gmt 0)

Ok cool, I didn't know you could use regular expressions in MySQL. However, when I put that in I get an error in my php, "unexpected '['". I'd assume I have to escape some of those characters...do you know which ones?

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved