GROUP BY conditions
Posted: Fri Apr 09, 2010 5:55 pm
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.
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.