Re: How to fetch a "group by" Query?
| From: | Chris Lambert - WhiteCrown Networks | Date: | Wed, 11 Jul 2001 08:06:43 +0000 |
| Subject: | Re: How to fetch a "group by" Query? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-57323@lists.php.net to get a copy of this message | ||
Couldn't you just do something like:
<?
$rows = mysql_query("SELECT * FROM table ORDER BY category, title");
while ($row = mysql_fetch_array($rows)) {
if ($row[title] <> $last)
print "<b>$row[category]</b><br>\r\n";
print "<li>$row[title]</li><br>\r\n";
$last = $row[title];
}
?>
/* Chris Lambert, CTO - chris@whitecrown.net
WhiteCrown Networks - More Than White Hats
Web Application Security - www.whitecrown.net
*/
----- Original Message -----
From: Tim Ward <tim.ward@stivesdirect.com>
To: <huntsman@www.nisu.flinders.edu.au>; Frédéric Mériot
<frederic@argyro.net>; PHP General List <php-general@lists.php.net>
Sent: Wednesday, July 11, 2001 4:00 AM
Subject: RE: [PHP] How to fetch a "group by" Query?
this is the sort of thing I usually do ...
$result = mysql_query($db, "SELECT * FROM filename...")
while ($array = mysql_fetch_array($result)) $category[$array["category"]][]
= $array["title"];
foreach ($category as $cat=>$title_array)
{ echo("Category $cat<br>");
foreach ($title_array as $title) echo(" - $title<br>");
}
a lot simpler and you don't have to worry about any start and end
conditions. This can also be extrapolated to more complex situations, e.g.
where $title contains more than just a name you can do sub totals of "title"
quantities by category, or whatever.
Tim Ward
Senior Systems Engineer
Please refer to the following disclaimer in respect of this message:
http://www.stivesdirect.com/e-mail-disclaimer.html
> -----Original Message-----
> From: David Robley [mailto:huntsman@www.nisu.flinders.edu.au]
> Sent: 11 July 2001 02:17
> To: Frédéric Mériot; PHP General List
> Subject: Re: [PHP] How to fetch a "group by" Query?
>
>
> On Tue, 10 Jul 2001 22:10, Frédéric Mériot wrote:
> > Hello (again)
> >
> > I've got a query which extract titles and categories with a
> group by on
> > the categorie. Is there a simple way to display rows like
> this (without
> > doing a second query ):
> >
> > Categorie A
> > -titi
> > -toto
> > -tutu
> >
> > Categorie B
> > -bibi
> > -nini
> > -fififi
> >
> > Categorie C
> > -titi
> > -toto
> > -tutu
> >
> > ... etc
> >
> > With cold fusion (for those who know) I want to do the same as
> > <CFOUTPUT QUERY="myquery" GROUP="categorie">
> >
> > Thanks
>
> Select category, description from table where whatever order
> by category,
> description
>
> Then when you display the rows, keep track of the current and
> previous
> value of $description; if it changes, print the new value.
>
> --
> David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc
> CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA
>
> Don't stop posting, a good laugh breaks up my day nicely
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net