List:General Discussion« Previous MessageNext Message »
From:Jim Faucette Date:May 30 1999 8:37am
Subject:Re: Multiple counts as an array
View as plain text  
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...
Thread
Multiple counts as an arrayDarren Sweeney30 May
  • Re: Multiple counts as an arrayJim Faucette30 May
  • Re: Multiple counts as an arrayAlexander I. Barkov30 May
    • LIKE or = ?Stefan Schmiedl31 May
  • Re: Multiple counts as an arrayJim Faucette30 May
  • Re: Multiple counts as an arrayGraham Ashton30 May
Re: Multiple counts as an arrayDarren Sweeney30 May
  • Re: Multiple counts as an arrayJim Faucette30 May
  • Re: Multiple counts as an arrayJim Faucette30 May
Re: Multiple counts as an arrayDarren Sweeney30 May
  • Re: Multiple counts as an arraySasha Pachev30 May