我有一个如下表,我想显示最新条目

时间:2018-07-27 10:29:10

标签: mysql sql

我现在使用的查询如下:

select ur.uid
     , ua.user_activity_min_budget
     , ua.user_activity_max_budget
     , ua.user_activity_bedroom
     , ptm.property_type_description
     , cm.city_name
     , lm.locality_name
     , ua.user_activity_datetime
  from user_registration ur
  join ksl_user_activity ua 
    on ua.registered_user_uid = ur.uid 
   and ua.user_activity_uid = ( select max(ua0.user_activity_uid) from ksl_user_activity ua0)
  join ksl_locality_master lm 
    on lm.locality_uid = ua.user_activity_area
  join ksl_city_master cm 
    on cm.city_uid = lm.city_uid
  join ksl_property_type_master ptm 
    on ptm. property_type_uid = ua.user_activity_property_type
 where date(ua.user_activity_datet±me) >= '20l7-07-24' 
   and (lm.city_uid = 1 or lm.city_uid=2)
 order 
    by ur.uid

此图所示的原始输出s:

enter image description here

数据是我现在得到的,但是我想要uid 3、15、33的最新条目

我执行以下操作的原因是和ua.user_activity_uid=(select max(ua0.user_activity_uid) from user_activity ua0)

ksl_user_activity表具有主键user_activity_id,该主键具有最新条目的最大值,但是当我将其包含在查询中时我没有得到任何数据。

我也尝试过ua.user_activity_uid=(select ua0.user_activity_uid from user_activity ua0 order by ua0.user_activity_uid desc limit 1)

这也不起作用。

1 个答案:

答案 0 :(得分:1)

使用max()函数和子查询

    select t1.uid from user_activity t1
    inner join 
    (select uid,max(user_activity_datetime) as user_activity_datetime from   user_activity group by uid
    ) as t2 on  
    t1.user_activity_datetime=t2.user_activity_datetime
    and t1.uid=t2.uid