3

I am trying to get data from my table using group by . using group by works correctly but i need to take only last inserted item of every group but its not work. my query always return first item of each group.

my query

SELECT id,type,userId,performDate,eventId FROM
`user_marker` where  `eventId`='842' and DATE_FORMAT(from_unixtime(performDate),'%Y%c%d')
=DATE_FORMAT(now(),'%Y%c%d') 
 and `visibility`='1'GROUP BY type ORDER BY id DESC
Sajidkhan
  • 11
  • 7
Portle74
  • 51
  • 4

2 Answers2

0

Please try

SELECT a.* FROM ( SELECT id,type,userId,performDate,eventId FROM 
`user_marker` where  `eventId`='842' and DATE_FORMAT(from_unixtime(performDate),'%Y%c%d')
=DATE_FORMAT(now(),'%Y%c%d') and `visibility`='1' ORDER BY id DESC ) as a GROUP BY a.type 
Suman Singh
  • 1,379
  • 12
  • 20
0

You can try as per below-

SELECT b.id,b.type,b.userId,b.performDate,b.eventId 
FROM user_marker b 
JOIN (SELECT `type`,MAX(performDate) 
FROM user_marker 
WHERE  `eventId`='842' AND DATE(FROM_UNIXTIME(performDate))=CURDATE() AND `visibility`='1' 
GROUP BY `type`) a ON a.type=b.type AND a.performDate=b.performDate 
ORDER BY b.`type`;
Zafar Malik
  • 6,734
  • 2
  • 19
  • 30