monkeytooth Posted July 2, 2011 Share Posted July 2, 2011 Whats the best way to handle a query that can range from 1 to 500+ OR's. Ok what I mean to say is.. I have a dynamic query that is checking my DB for active products. I want to take any random set of product numbers which can literally range from 1 number to 500+ numbers at a time. So that said I am creating the dynamic aspect of my query via PHP but I am wondering is there a better way to query. 1000,5020,6060,8888,9893... then below snip SELECT * FROM myTable WHERE id=1000 OR id=5020 OR id=6060 OR id=8888 OR id=9893 Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/ Share on other sites More sharing options...
monkeytooth Posted July 2, 2011 Author Share Posted July 2, 2011 Also worth mentioning is I am checking to see if the numbers still exist or not. If the number does then I want to dump numbers found in the DB still into one array, and then numbers not found anymore I want to dump that number into another array.. Is a query like Im attempting to do even capable of that or is this something I have to literally query the DB 1-500+ times per poll.. I can't really picture that being highly optimized so I am hoping for the latter.. Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237551 Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2011 Share Posted July 2, 2011 http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237583 Share on other sites More sharing options...
monkeytooth Posted July 2, 2011 Author Share Posted July 2, 2011 Ok so I'm not sure how I would use that, or if that really applies. I'm going to take 1 number up to 500 or more numbers and for a list. So would I do it something like... $myVar = 1000,5020,6060,8888,9893; //where this var can be up to 500 or more number sets easy.. SELECT productID FROM myTable WHERE productID IN ($myVar) Will that return an array or something where it says true/false per number from $myVar Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237590 Share on other sites More sharing options...
Pikachu2000 Posted July 2, 2011 Share Posted July 2, 2011 Perhaps you should explain the broader picture of what it is you're trying to accomplish here. Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237592 Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2011 Share Posted July 2, 2011 Will that return an array or something I vote for the or something. It will return a result resource consisting of the set of rows that were matched. The values that weren't matched (didn't exist in the table) won't exist in the result set. Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237600 Share on other sites More sharing options...
monkeytooth Posted July 2, 2011 Author Share Posted July 2, 2011 Ok, I'll stop putting it into "book" terms.. with mention of products.. What I am ultimately doing is getting peoples contact lists, gmail, facebook, hotmail, aol, etc.. I am getting a list of email address's or in FB's case ID numbers. Where those lists can be fairly large. What I am I really trying to do is take those lists and compare them to a table in my DB to see if theres anyone in my DB (or not) with the same address/id. So people can say hey I know him, I didn't know he used this site too.. Problem is those lists can get very large, I know I can trim the size of the list down to whatever I want but more or less I feel the more the merrier. I also know looping through a DB 500+ revolutions on a per id/email basis isn't the best idea either, chances are high I will crash the DB time and time again when those numbers reach a certain point and more so if there are enough people on the site doing it at once. So that's it in a nutshell no more obfuscating in a sense of speaking what im trying to do by using something I find more people on here relate to.. Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237602 Share on other sites More sharing options...
monkeytooth Posted July 2, 2011 Author Share Posted July 2, 2011 Alright so I messed with the idea of IN I am liking it. So I guess my next question is would it be wise to do 2 similar queries.. one IN() one NOT(IN()) so I can form essentially 2 arrays of people found, vs people not found or would it be wiser to just do the IN() or the NOT(IN()) and then wok with the array via php comparing whats found/not found to the array i formed to make the initial query with Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237631 Share on other sites More sharing options...
Pikachu2000 Posted July 2, 2011 Share Posted July 2, 2011 Well, from the sounds of it, using IN() would be your best bet. Just put the contacts in an array, and implode the array into the IN() function in the query string. The query will return results that match any of the values in the IN clause. $contacts = range('a', 'z'); // just as an example $list = implode("', '", $contacts); $query = "SELECT field FROM table WHERE contact IN( '$list' )"; Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237633 Share on other sites More sharing options...
fenway Posted July 2, 2011 Share Posted July 2, 2011 Over 500? How did this random list of IDs get generated? Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237737 Share on other sites More sharing options...
monkeytooth Posted July 2, 2011 Author Share Posted July 2, 2011 Through gmail contact list, or FB friend ID's Quote Link to comment https://forums.phpfreaks.com/topic/240925-whats-the-best-way-to-handle-a-query-that-can-range-from-1-to-500-ors/#findComment-1237751 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.