Table Structure
id | term | result
==================
1 | t1 | 0
2 | t2 | 0
3 | t1 | 34
4 | t1 | 23
5 | t2 | 10
6 | t2 | 10
7 | t3 | 20
The table is for a search table where term is the keyword someone was searching for, and result is the number of results returned at that time. Now from this table I need to retrieve
1) number of times a term was searched for and
2) the number of results that were returned when it was last search for.
for the first one I can do count(term) group by term, and for 2 probably ORDER by id DESC, but I don’t know how to do both in a single query. Any help?
You want the groupwise maximum:
See it on sqlfiddle.