Shoutbox

Number Range Query - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Number Range Query (/showthread.php?tid=56358)

Number Range Query by Dempsey on 03-01-2006 at 12:50 PM

Just wondering if anyone can help me out a bit with this stuff I'm doing at work?

Well if I have a list of numbers, something like:

quote:
29, 32, 33, 37, 45, 78, 82, 200, 678, 680, 683, 690, 705

I then want to perform an SQL query where a field is one of those numbers, but as the list could be a lot larger, I can't just list the numbers in the query as I'm guessing there would be a limit.

So what would be the way to programatically create a 'smart query' so that its something like:

quote:
WHERE (blah >28 and blah < 46) or (blah is 82 or 82) or (blah > 677 and blah < 706)

If anyone has any ideas how'd I'd go about it or about the limits of SQL queries to an IBM machine then please reply  :)

I'll be doing this in FileMaker scripting, but if really necessary I could add it to our existing VB app which is also part of the system.  But any advice even how'd you would do it in other languages would be useful.

Thanks for any replies  :D
RE: Number Range Query by RaceProUK on 03-01-2006 at 01:48 PM

quote:
WHERE (blah >28 and blah < 46) or (blah is 82 or 82) or (blah > 677 and blah < 706)
will work.
RE: RE: Number Range Query by Dempsey on 03-01-2006 at 02:32 PM

quote:
Originally posted by raceprouk
quote:
WHERE (blah >28 and blah < 46) or (blah is 82 or 82) or (blah > 677 and blah < 706)
will work.

well I know thats a valid query, that wasnt my question, and that was just an example list, much smaller than the real data.  I asked how I could create that query programmatically.
RE: Number Range Query by Adeptus on 03-01-2006 at 05:34 PM

Dempsey,

If you have a list of values you want, SQL has the IN operator:

SELECT * FROM foo WHERE bar IN (29, 32, 33, 37, 45, 78, 82, 200, 678, 680, 683, 690, 705)

The list can result from a subquery, so if your list is too long to specify as literals, you could create a temporary table, insert all the values you want in it, and then:

SELECT * FROM foo WHERE bar IN (SELECT bar FROM tmptable)

Of course, if the list is somehow derived from the database, you should just use that query directly and avoid the need for temporary tables.  This is where the context of what you are doing becomes important -- unless the arbitrary list is truly arbitrary (e.g. user selected), there probably is a better way.


RE: Number Range Query by Dempsey on 03-01-2006 at 05:54 PM

Ah cool cheers Adeptus thats probably what I'll do.  The list of numbers  are like IDs from another table so I can just do the subquery and then I'll recieve only the exact records I want.


RE: Number Range Query by Adeptus on 03-01-2006 at 06:34 PM

Great!

By the way, if your subquery doesn't contain aggregates, you can also accomplish the same with a join:

SELECT * FROM foo INNER JOIN boo ON foo.bar = boo.bar AND boo.baz > 69

...which would accomplish the same as:

SELECT * FROM foo WHERE bar IN (SELECT bar FROM boo WHERE baz > 69)

There is no overwhelming reason to do it this way, but it may be slightly faster and it is the only option for cheesy databases that don't support subqueries (older MySQL).