Welcome to WebmasterWorld Guest from 23.23.46.20

Forum Moderators: open

Get column if it has a word

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

5+ Year Member



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".

Thanks.

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

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



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):

SELECT *
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)

5+ Year Member



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?
 

Featured Threads

Hot Threads This Week

Hot Threads This Month