Alexander I. Barkov wrote:
>
> Hi!
>
> > I need to search one table for about twelve different items and return =
> > the number found for each one ... preferably in an array that I can =
> > simply call as e.g. $count[0], $count[1] etc. etc.
> >
> > At the moment I am doing:
> >
> > my $query =3D "select count(*) FROM items WHERE ( category like =
> > \"pc_digital_cameras\" )";
> > my $sth =3D $dbh->prepare($query);
> > my $rc =3D $sth->execute;
> > $digcam =3D $sth->fetchrow;
> > $sth->finish;
> >
> > my $query =3D "select count(*) FROM items WHERE ( category like =
> > \"pc_memory\" )";
> > my $sth =3D $dbh->prepare($query);
> > my $rc =3D $sth->execute;
> > $mem =3D $sth->fetchrow;
> > $sth->finish;
> >
> > I'm thinking there must be a more structured and organized way to do =
> > this rather than running 12 seperate queries.
>
> I think that this qyery is what you want:
>
> SELECT
> sum(category LIKE "pc_digital_camers") as count0,
> sum(category LIKE "pc_memory") as count1,
> ...........
> sum(caterogy LIKE "something_else") as count12
> FROM
> items;
>
> ---
Alexander,
That's VERY nice. It's one to remember!!!
jim...