我有一个像user_places这样的表:
user_id recorded_date place_visited
2 2016-04-05 NY
2 2016-04-07 UK
2 2016-04-08 UK
2 2016-04-08 UK
3 2016-04-08 AUS
3 2016-04-09 AUS
2 2016-04-15 NY
我正在尝试获取用户的最新record_date以及他访问过的地点的名称。
答案 0 :(得分:1)
如果您的数据不是太大,那么您可以在MySQL中使用这个技巧:
select user_id, max(dte),
substring_index(group_concat(place_visited order by cnt desc), ',', 1)
from (select user_id, place_visited, count(*) as cnt, max(recorded_date) as dte
from user_places
group by user_id, place_visited
) upv
group by user_id;