List:General Discussion« Previous MessageNext Message »
From:Jim Faucette Date:May 30 1999 2:56am
Subject:Re: Multiple counts as an array
View as plain text  
Darren Sweeney 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 = "select count(*) FROM items WHERE ( category like \"pc_digital_cameras\"
> )";
>  my $sth = $dbh->prepare($query);
>  my $rc = $sth->execute;
>  $digcam = $sth->fetchrow;
>  $sth->finish;
> 
>  my $query = "select count(*) FROM items WHERE ( category like \"pc_memory\" )";
>  my $sth = $dbh->prepare($query);
>  my $rc = $sth->execute;
>  $mem = $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.
> 

Maybe you're looking for the GROUP BY syntax (multiple rows returned):
select category, count(*) FROM items GROUP BY category;

  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