MYSQL: select latest record only (on left join table)

前端 未结 4 1337
时光说笑
时光说笑 2021-01-17 22:42

I have 2 tables:

Table1:

ID | Mobile Number | Name | Ordered Product| Order Date

Table2:

         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 23:26

    Try this ... I hope it may works

    select a.* , b1.* 
    FROM table1 a 
    LEFT JOIN table2 b1
    ON ( a.ID=b1.ID)  
    LEFT JOIN table2 b2
    ON ( a.ID=b1.ID)  AND 
    (b1.Time < b2.Time OR b1.Time = b2.Time AND b1.ID < b2.ID)
    WHERE b2.ID IS NULL
    GROUP BY a.ID ORDER BY b1.Time DESC
    

提交回复
热议问题