Re: Querying a database for 50 users' information: 50 queries or a WHERE array?
| From: | Steve Staples | Date: | Tue, 13 Sep 2011 18:06:38 +0000 |
| Subject: | Re: Querying a database for 50 users' information: 50 queries or a WHERE array? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314802@lists.php.net to get a copy of this message | ||
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