Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Get column if it has a word

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

New User

5+ Year Member

joined:Oct 4, 2009
posts: 26
votes: 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)

Senior Member from US 

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Dec 9, 2003
votes: 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)

New User

5+ Year Member

joined:Oct 4, 2009
posts: 26
votes: 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?

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members