我有一个表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
我正在尝试获取用户的最新recorded_date和他最常访问的地点的名称。
如果数据不是太大,则可以在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;