0

I've been trying to gather some data from a MySQL database for a while now.

The problem is that I want to group the data by objectid while getting only the newest data (Descending).

Everytime I try it, I either get an error, or it doesnt Descend.

My query at the moment is:

SELECT * FROM 'table' GROUP BY 'objectid' DESC

I've tried ordering by id or timestamp, but with an ascended result.

My question is similar to: http://stackoverflow.com/questions/7306082/mysql-using-group-by-and-desc

However the answers provided there didn't solve my problem.

Thanks in advance!

Resitive
  • 280
  • 1
  • 6
  • 24

3 Answers3

2

You also have to specify ORDER BY clause for sorting:

SELECT * FROM 'table' GROUP BY 'objectid' ORDER BY timestamp DESC
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
0

your query is wrong you should make backticks instead of single quotes and you missed order by statment

try this

     SELECT * FROM `table` GROUP BY `objectid` Order by id DESC
echo_Me
  • 37,078
  • 5
  • 58
  • 78
  • I've only used single quotes because `backticks` are used for different purposes on stackoverflow ;) – Resitive May 27 '13 at 10:05
0

This will give you the most recent entry per ObjectId, ordered by descending time; it simply left joins to find all entries where there exists no newer entry for the same ObjectId.

SELECT a.* 
FROM table1 a
LEFT JOIN table1 b
  ON a.ObjectId=b.ObjectId 
 AND a.time < b.time
WHERE b.ObjectId IS NULL
ORDER BY time DESC

An SQLfiddle to test with.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294