0

Here is a sample data set.

id list_id user_id times
 2       1       8 2017-05-25 10:27:18
 4       2       8 2017-05-26 12:58:58
 6       1       8 2017-06-09 17:28:00
 7       2       8 2917-06-09 19:27:36

I have multiple list_id in table. i want to fetch only a single list_id on the basis of latest date. Please help me guys. I am using this query but i am not succeed.

 select * 
  from class_history 
 where user_id=8 
 GROUP 
  BY list_id 
 ORDER by id DESC 

or

select *
   from class_history 
 where user_id=8 
 GROUP 
    BY list_id 
 ORDER by time DESC 

Thanks for your support.

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Vivek Mishra
  • 37
  • 1
  • 7

1 Answers1

-1

You have distinct id's so you won't be able to get distinct list_id's, you would need to do:

SELECT B.* FROM
(
    select list_id,user_id ,max(times) times
    from class_history 
    where user_id=8
    group by list_id 
) A INNER JOIN class_history B USING (list_id,user_id,times);
ka_lin
  • 9,329
  • 6
  • 35
  • 56