What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Number Range Query

Number Range Query
Author: Message:
Dempsey
Scripting Contest Winner
*****

Avatar
http://AdamDempsey.net

Posts: 2395
Reputation: 53
37 / Male / Flag
Joined: Jul 2003
O.P. Number Range Query
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
SoundPacks   -   Scripts   -   Skins

that's not a bug, thats an unexpected feature
03-01-2006 12:50 PM
Profile E-Mail PM Web Find Quote Report
RaceProUK
Elite Member
*****

Avatar

Posts: 6073
Reputation: 57
39 / Male / Flag
Joined: Oct 2003
RE: Number Range Query
quote:
WHERE (blah >28 and blah < 46) or (blah is 82 or 82) or (blah > 677 and blah < 706)
will work.
[Image: spartaafk.png]
03-01-2006 01:48 PM
Profile PM Web Find Quote Report
Dempsey
Scripting Contest Winner
*****

Avatar
http://AdamDempsey.net

Posts: 2395
Reputation: 53
37 / Male / Flag
Joined: Jul 2003
O.P. RE: RE: Number Range Query
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.
SoundPacks   -   Scripts   -   Skins

that's not a bug, thats an unexpected feature
03-01-2006 02:32 PM
Profile E-Mail PM Web Find Quote Report
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: Number Range Query
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.

03-01-2006 05:34 PM
Profile E-Mail PM Find Quote Report
Dempsey
Scripting Contest Winner
*****

Avatar
http://AdamDempsey.net

Posts: 2395
Reputation: 53
37 / Male / Flag
Joined: Jul 2003
O.P. RE: Number Range Query
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.
SoundPacks   -   Scripts   -   Skins

that's not a bug, thats an unexpected feature
03-01-2006 05:54 PM
Profile E-Mail PM Web Find Quote Report
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: Number Range Query
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).
03-01-2006 06:34 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On