Re: Querying a database for 50 users' information: 50 queries or a WHERE array?
| From: | Alex Nikitin | Date: | Tue, 13 Sep 2011 18:34:07 +0000 |
| Subject: | Re: Querying a database for 50 users' information: 50 queries or a WHERE array? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314804@lists.php.net to get a copy of this message | ||
On Tue, Sep 13, 2011 at 2:06 PM, Steve Staples <sstaples@mnsi.net> wrote:
> On Tue, 2011-09-13 at 09:48 -0700, David Harkness wrote:
> > On Tue, Sep 13, 2011 at 7:29 AM, Ashley Sheridan
> > <ash@ashleysheridan.co.uk>wrote:
> >
> > > SELECT * FROM table WHERE userID IN (1,2,3,4,5,etc)
> > >
> >
> > +1. And this is a great place to use implode():
> >
> > $sql = 'select ... where userID in (' . implode(',', $ids) .
> > ')';
> >
> > David
>
> I mentioned that implode earlier, but there is also the underlying
> question (which I also asked earlier)... how is he getting the 50 id's
> to populate?
>
> here are 2 other ways of skinning the cat:
>
> using an inner join:
> select table.* from table inner join othertable on (table.userid =
> othertable.userid) where (use the way your getting the 50 id's here);
>
> OR by using a subselect,
> select * from table where userid IN (select group_concat(userid,
> separator ', ') FROM othertable where (using logic here));
>
> guess it all depends on how you want to do it... but that would make it
> 1 db query
>
> good luck!
>
>
> --
>
> Steve Staples
> Web Application Developer
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
And this will be faster or at least more efficient with a limit (e.g. limit
50) this way when you have found the 50 users in the "in" statement, you
don't continue iterating through the rest of your data set...
--
The trouble with programmers is that you can never tell what a programmer is
doing until it’s too late. ~Seymour Cray