I have a table like this:
-----------------------------
id | uid | year | other | many | fields
-----------------------------
1 | 1 | 2010 | blabla ...
2 | 2 | 1999 | blablabla ...
3 | 3 | 2011 | bla ...
4 | 1 | 2006 | blablablabla ...
...
-----------------------------
What I want is to select all fields in all records that
has distinct
uid
and only returns the last record (i.e., has the highestid
)the results are sorted by year
An example of returned records like:
-----------------------------
id | uid | year | other | many | fields
-----------------------------
2 | 2 | 1999 | blablabla ...
4 | 1 | 2006 | blablablabla ...
3 | 3 | 2011 | bla ...
-----------------------------
It looks like similar to question How to use DISTINCT and ORDER BY in same SELECT statement? but I couldn't get it work.
I tried SELECT * FROM table GROUP BY uid ORDER BY MAX(id) DESC, MAX(year)
, but it seems neither sorting id
nor year
.
update:
Thanks for all solutions, here is the new problem: I'm actually developing plugin in Discuz, and it doesn't allow sub queries for security reason, is there any way to use only one select? Or any workaround in Discuz plugin development? Thanks again.