Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-28-2013, 02:56 PM   PM User | #1
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 70
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Shortening my query?

Hello, I'm looking to run a query which runs a regular expression query on three columns searching for a term and the plural variety on the term. So if I was looking for the word building my search would look like this

Code:
SELECT col1,col2,col3 FROM `table`
WHERE col1 REGEXP '[[:<:]]building[[:>:]]'
OR col2 REGEXP '[[:<:]]building[[:>:]]'
OR col3 REGEXP '[[:<:]]building[[:>:]]'
OR col1 REGEXP '[[:<:]]buildings[[:>:]]'
OR col2 REGEXP '[[:<:]]buildings[[:>:]]'
OR col3 REGEXP '[[:<:]]buildings[[:>:]]'
Now as you can see my query is quite long and if I wanted to add additional columns to search columns 4,5 & 6 it would get longer still. So my question is please there anyway I could shorten this type of query to make it faster or is this the best way?

Thank you
Oatley is offline   Reply With Quote
Old 02-28-2013, 07:19 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
For starters, why the regexp???

It would be much more efficient to use LIKE if you don't really need the regexp.

But if you do use the regexp, what's wrong with simply
Code:
col1 REGEXP '[[:<:]]buildings?[[:>:]]'
The ? after the s makes that character optional.

If you needed to make a group optional and with alternatives:
Code:
col1 REGEXP '[[:<:]]compan(y|ies)?[[:>:]]'
which would match
company
companies
compan
[Okay, no such thing as "compan", but I couldn't thing of a better example.]
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
Oatley (03-01-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:19 AM.


Advertisement
Log in to turn off these ads.