homepage Welcome to WebmasterWorld Guest from 54.204.141.129
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
Get column if it has a word
radiator251




msg:4071224
 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".

Thanks.

 

whoisgregg




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

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

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

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

radiator251




msg:4071570
 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