Re: sorting results in PHP
| From: | bill | Date: | Mon, 17 Mar 2003 18:21:36 +0000 |
| Subject: | Re: sorting results in PHP | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-139992@lists.php.net to get a copy of this message | ||
Aha.
Comments like yours made me look again. It turns out that I could use ORDER BY
with a GROUP BY but I couldn't have it before a HAVING.
Specifically, I had
SELECT ...
GROUP BY $detailsdb.eid
ORDER BY Year, Month, Day
HAVING foobar = $numcatchoices
To fix it I changed the order to
SELECT ...
GROUP BY $detailsdb.eid
HAVING foobar = $numcatchoices
ORDER BY Year, Month, Day
thanks,
bill
"John W. Holmes" wrote:
> > Sorting like that doesn't get it sorted by date. Each row has a lot
> of
> > fields. It
> > actually needs to be sorted three times by three fields, Year, Month,
> and
> > Day.
> >
> > Because the query uses a GROUP BY statement, I can't sort it in the
> query.
>
> Why? I still don't see why you can't use an ORDER BY clause.
>
> ORDER BY date_column DESC
>
> Which will order by year, month, then day. The order by is applied after
> the GROUP BY, which will accomplish the same thing that you're trying to
> do in PHP, only much faster.
>
> ---John W. Holmes...
>
> PHP Architect - A monthly magazine for PHP Professionals. Get your copy
> today. http://www.phparch.com/
>
> > André cupini wrote:
> >
> > > Bill
> > > Try this:
> > >
> > > while ($crow=mysql_fetch_array($cresult)) {
> > > $therow[]=$crow;
> > > }
> > > sort($therow);
> > >
> > > André Cupini
> > > Programador
> > > andre@neobiz.com.br
> > > NeoBiz - fábrica de internet
> > > http://www.neobiz.com.br
> > >
> > > ----- Original Message -----
> > > From: bill
> > > To: php-general@lists.php.net
> > > Sent: Monday, March 17, 2003 11:54 AM
> > > Subject: [PHP] sorting results in PHP
> > >
> > > I have a query that returns results including the fields Year,
> Month,
> > and Day
> > > that I want to sort by date.
> > >
> > > Because of the nature of the query (it includes a GROUP BY
> statement),
> > I cannot
> > > sort it in the query.
> > >
> > > How can I sort the results?
> > >
> > > I tried to use asort() while designating the field but that didn't
> > work.
> > >
> > > while ($crow=mysql_fetch_array($cresult)) {
> > > $therow[]=$crow;
> > > }
> > > asort($therow["Year"]);
> > > reset($therow);
> > > asort($therow["Month"]);
> > > reset($therow);
> > > asort($therow["Day"]);
> > > reset($therow);
> > >
> > > ideas?
> > >
> > > kind regards,
> > >
> > > bill
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php