Mysql Select distinct records from latest dates only

后端 未结 2 715

This is my table structure -

TABLE : COURSE_LOG

-----------------------------------------------------
|   ID   |   USERNAME   |  COURSE_ID   |    LDATE   |         


        
2条回答
  •  时光说笑
    2020-12-10 08:59

    Try this query

    If you want only for user1 then use this query:

    select username, course_id, max(ldate) as date
    from tbl 
    where username='user1'
    group by username, course_id
    

    SQL FIDDLE

    | USERNAME | COURSE_ID |       DATE |
    -------------------------------------
    |    user1 |        22 | 2013-06-03 |
    |    user1 |        54 | 2013-06-03 |
    

    If you want to find the latest date for all users then use this query

    select username, course_id, max(ldate) as date
    from tbl 
    group by username, course_id
    

    In this query data of user2 will also be included

    | USERNAME | COURSE_ID |       DATE |
    -------------------------------------
    |    user1 |        22 | 2013-06-03 |
    |    user1 |        54 | 2013-06-03 |
    |    user2 |        71 | 2013-06-04 |
    

提交回复
热议问题