如何从mysql中的同一个表中的最大日期获取最大时间

时间:2017-01-13 10:44:05

标签: mysql

select sync_date, max(sync_end_time),user_code from sync_tracker where 
sync_action = 'COMPLETION' 
and sync_status = 'SUCCESS'
and user_code in (select ut.code from user_table ut
   join user_location_mapping ulm
   on ut.code = ulm.user_code) 
and sync_date = MAX(sync_date)-- can't even write aggregate function
group by sync_date,user_code
order by sync_date desc

User_code recent_login_date recent_login_time
0012      2016_11_11        11:30:23
0013      2016_1_19         12:30:23
0014      2016_12_4         1:30:23

我想要最近的日期和时间,这些是不同的列。 请帮我找出相同的信息,它给出错误,我应该使用'EXISTS'作为sbquery返回多个记录。

1 个答案:

答案 0 :(得分:0)

    select st.user_code, tAux.dateMax, max(st.sync_end_time) as timeMax
    from sync_tracker st
    inner join 
    (
    select user_code, max(sync_date) as dateMax
    from sync_tracker 
    inner join user_table ut on user_code = ut.code
    inner join user_location_mapping ulm on ut.code = ulm.user_code
    where sync_action = 'COMPLETION' 
    and sync_status = 'SUCCESS'
    group by user_code, sync_date
    ) tAux
    on st.user_code = tAux.user_code and st.sync_date = tAux.dateMax
    group by st.user_code, tAux.dateMax
    order by dateMax, timeMax