Re: Querying a database for 50 users' information: 50 queries or a WHERE array?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2011-09-13 at 17:24 +0300, Dotan Cohen wrote:
> I have a MySQL database table with about 10,000 rows. If I want to
> query for 50 specific users (so no LIMIT ORDER BY) then I seem to have
> these choices:
> 
> 1) SELECT * FROM table
> This will pull in all 10,000 rows, not nice!
> 
> 2) foreach ($user as $u) { mysql_query("SELECT * FROM table WHERE
> userID=".$u);  }
> This will lead to 50 queries, again not nice! (maybe worse)
> 
> 3) foreach ($user as $u) { $whereClause+=" OR userID=".$u; }
> This makes a huge SQL query. However, this is the method that I'm using now.
> 
> Is there some sort of array that can be passed in the WHERE clause,
> containing all the userID's that I am interested in?
> 
> Thanks!
> 
> -- 
> Dotan Cohen
> 
> http://gibberish.co.il
> http://what-is-what.com
> 

what criteria are you using to get the "stats" for these 50 users?

also, wouldn't this be much better suited for the mysql mailing list?


if you know all the userids, then you could just do it as:

$sql = "SELECT * FROM table WHERE userid IN (". implode(', ',
$usersids) .")";

not very elegant, and I am not sure that the IN is any better than doing
50 mysql calls, but this is only 1 call, and gets you the data.

Are you querying the database to get the id's in the frist place?  if
so, you could look at doing an inner join on the 2 tables.

-- 

Steve Staples
Web Application Developer


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux