Jump to content

Whats the best way to handle a query that can range from 1 to 500+ OR's


Recommended Posts

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

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..

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

 

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.

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..

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

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' )";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.