PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 14th, 2002, 10:19 PM
Ted Striker Ted Striker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 409 Ted Striker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Question Need some help with a basic LIKE % statement

Hi everybody.

I'm trying to formulate a query for a keyword search engine.

I have the following query:

PHP Code:
 SELECT title FROM books WHERE UPPER(descriptionLIKE '%LOSE%'


description is varchar(10000)

This query will return all books with the description containing words such as:

1) closely
2) loser
etc.

This is the behavior I would expect. But now I want to take it to the next level. Basically, I want to find only the literal instances of the word LOSE. Not just all instances of the character string within any string.

I'd like to find only the word LOSE, when it is:
1) Surrounded by whitespace, or
2) surrounded by the any of the following characters: !~@#$%^&*()-+=/?<>"':;,.`\|[]{}_

I've tried using IN or = instead of LIKE, but no luck. I also removed the % wildcard but still no luck. These all come up with no results found.

I've also tried:

PHP Code:
 SELECT title FROM books WHERE UPPER(descriptionLIKE '_LOSE_'


and just

PHP Code:
 SELECT title FROM books WHERE UPPER(descriptionLIKE 'LOSE'



Do I need to do some stuff with PHP before sending the query word to the database? I am thinking because this is a long varchar(10000), that's why I can't figure it out.

Hmmm....any ideas?

Thanks,

Ted

Last edited by Ted Striker : June 14th, 2002 at 10:29 PM.

Reply With Quote
  #2  
Old June 15th, 2002, 05:55 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
Side note: PostgreSQL now supports ILIKE, for case insensivite LIKE queries.

Main answer:

You can use regular expressions in PostgreSQL queries. There is a very neat special bracket expression (fifth from last paragraph) that allows you to specify the null string at the beginning and end of a word.

SELECT title FROM books WHERE description ~* '[[:<:]]lose[[:>:]]';

(~ means regular expression, * means case insensitive)

This bracket case is not completely POSIX-compliant, so you might want to do a more standard regex for portability:

SELECT title FROM books WHERE description ~* '[^a-zA-Z0-9]lose[^a-zA-Z0-9]';

The above bracketed class basically means "anything non alpha-numeric". (^ indicates negation, when used inside the brackets)

Also, for search engine work, you might find this full-text indexing tutorial interesting: http://techdocs.postgresql.org/tech...extindexing.php
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #3  
Old June 17th, 2002, 12:00 AM
Ted Striker Ted Striker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 409 Ted Striker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Thumbs up

rycamor,

Thanks once again for being generous with your time and knowledge. I implemented the portable version of that code and it works out perfectly.


I'm going to check out that text indexing tutorial and see what's going on there. That's hot off the presses, only about 2 months old.

Thanks a million.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Need some help with a basic LIKE % statement

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap