Page 1 of 1

GROUP BY conditions

Posted: Fri Apr 09, 2010 5:55 pm
by pickle
Hi all,

I'm doing a query on a table. One of the columns, `type` is one of 3 values, the rest can be entirely unique/arbitrary. Right now, I'm doing a GROUP BY on `type`, because I only want one row returned per `type` value. MySQL is giving me my 3 rows, but it's always returning the first row for a given type. I want the last row.

So, if I've got 3 row of type "permanent", one with a `from_date` of 1995, one of 2000, and one of 2003, I want the row returned to be the 2003 row. I know I can brute force this by getting all rows and plugging through them in PHP, but I'm wondering if there's a way to do it just in the query.

Thanks.

Re: GROUP BY conditions

Posted: Fri Apr 09, 2010 6:54 pm
by Eran
I've written an article recently about how to achieve the results you are looking for, have a look:
http://www.techfounder.net/2010/03/12/f ... ith-mysql/

Re: GROUP BY conditions

Posted: Sat Apr 10, 2010 1:23 pm
by pickle
Looks promising. I was hoping to avoid sub-queries (rationally or not - they just seem like a cludge fix), but it seems like that's the only solution. Thanks.