首页 > 解决方案 > 如何获取数据库中的最新记录

问题描述

我需要从 deli_order 表中获取最新记录。我当前的表是这样的

order_code       |      code    |     create_time      | status
1914311385030918    nebula74920   2019-05-10 17:12:30      Fail
1914311385030918    nebula74920   2019-05-12 04:16:30      Fail
1914311385030918    nebula74920   2019-05-13 10:01:30      Success

有 3 条记录具有相同的 order_code 和 code,但状态不同。

我只需要生成成功的交易。以下是我的查询

select 
    a.order_code as 'TRANSACTION ID',
    max(a.create_time) as 'TRANSACTION DATE',
    a.code as 'ORDER ID',
    case
        when a.status='8' then 'Failed'
        when a.status='6' then 'Completed'
        when a.status='5' then 'To be reviewed'
        when a.status='3' then 'To be accepted'
        when a.status='2' then 'To be accepted'
    else 'N/A'
    end as 'BLUTAB STATUS',
    b.lp_sign_time as 'PICK UP TIME'   
 from deli_order a
 left join pg_send_package b on a.code = b.code and a.order_code = b.order_code
 where date_format(a.plat_create_time, '%Y-%m') = '2019-05'
 group by a.order_code, a.code;

我希望获得成功状态的交易。但这就是我得到的

order_code       |      code    |     create_time      | status
1914311385030918    nebula74920    2019-05-13 10:01:30   Fail

我应该在查询中更改什么?

标签: mysql

解决方案


推荐阅读