Page 1 of 1

query for value with allowance

Posted: Sat Apr 10, 2010 9:55 am
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

Re: query for value with allowance

Posted: Sat Apr 10, 2010 1:31 pm
by minorDemocritus

Code: Select all

SELECT * FROM `tablename` WHERE 'width' <= ( $width + 2 ) AND 'width' >= ( $width - 2 )

Re: query for value with allowance

Posted: Sat Apr 10, 2010 1:37 pm
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

Re: query for value with allowance

Posted: Sat Apr 10, 2010 2:01 pm
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.

Re: query for value with allowance

Posted: Sat Apr 10, 2010 2:04 pm
by minorDemocritus
...And please PLEASE use the [ syntax=sql ] bbcode so our eyes don't bleed 8O

Re: query for value with allowance

Posted: Sat Apr 10, 2010 2:10 pm
by glennnall
minorDemocritus wrote:...And please PLEASE use the [ syntax=sql ] bbcode so our eyes don't bleed 8O

you got it - sorry - thanks!