id | name | current_city | login_time |
---|---|---|---|
8 | A user | Shanghai | 2019-10-18 10:00:00 |
7 | A user | Beijing | 2019-10-18 09:00:00 |
6 | User B | Hangzhou | 2019-10-18 08:00:00 |
5 | A user | Tianjin | 2019-10-18 07:00:00 |
4 | User B | Jiangsu Province | 2019-10-18 06:00:00 |
3 | User C | Zhengzhou | 2019-10-18 05:00:00 |
2 | User B | Nanjing | 2019-10-18 04:00:00 |
1 | User C | Shenzhen | 2019-10-18 03:00:00 |
mysql data as shown above:
How to get the last occurrence of city information for each user
result:
|id| name |current_city |login_time |
| -- | ------ | ------------- | ------------------------ |
|8 | 用户A | 上海 | 2019-10-18 10:00:00 |
|6 | 用户B | 杭州 | 2019-10-18 08:00:00 |
|3 | 用户C | 郑州 | 2019-10-18 05:00:00 |
Subqueries initially also thought, forget, and later help v Station and a big God has given the following sub-queries, feel good, you can look
select name,current_city from table where id in
(
select max(id) from table
group by name
)