query for value with allowance

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
glennnall
Forum Newbie
Posts: 13
Joined: Wed Jan 27, 2010 7:54 am

query for value with allowance

Post by glennnall »

don't know how to search for what i'm looking for - i need to SELECT a numerical value from a table with a +/- allowance (WHERE 'width' = $width +/- 2). can someone help me with this simple query?

thanks much

GN
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: query for value with allowance

Post by minorDemocritus »

Code: Select all

SELECT * FROM `tablename` WHERE 'width' <= ( $width + 2 ) AND 'width' >= ( $width - 2 )
glennnall
Forum Newbie
Posts: 13
Joined: Wed Jan 27, 2010 7:54 am

Re: query for value with allowance

Post by glennnall »

I ended up with WHERE width (between ($w - 2) AND ($w + 2)) AND length (between ($l - 2) AND ($l + 2)); and it seems to work - is there any efficiency benefit to your way over "between"? PHP versions, etc...?

thanks for you help.

gn
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: query for value with allowance

Post by minorDemocritus »

That's hard to say. Try it! If you have phpMyAdmin, when you run direct SQL queries, it will tell you:

Showing rows 0 - 5 (6 total, Query took 0.0147 sec)

Do it a few times, though, and average the times to account for server load changes, etc.
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: query for value with allowance

Post by minorDemocritus »

...And please PLEASE use the [ syntax=sql ] bbcode so our eyes don't bleed 8O
glennnall
Forum Newbie
Posts: 13
Joined: Wed Jan 27, 2010 7:54 am

Re: query for value with allowance

Post by glennnall »

minorDemocritus wrote:...And please PLEASE use the [ syntax=sql ] bbcode so our eyes don't bleed 8O

you got it - sorry - thanks!
Post Reply