我有重复的phone_id,但名称不同,在我的数据库中,我有3列,phone_id,名称,created_at
1. phone_id: 111, name: David, created_at: 10:30
2. phone_id: 122, name: Shinta, created_at: 7:10
3. phone_id: 111, name: Juan, created_at: 11:36
我喜欢只获取最后一个created_at
所以结果是
phone_id: 111, name: Juan, created_at: 11:36
phone_id: 122, name: Shinta, created_at: 7:10
当我使用时:
*SELECT * FROM Contacts GROUP BY phone_id*
它得到了David和Shinta的结果,因为David是第一次创建,但这是我不想要的结果
我希望结果是Juan和Shinta 根据上次创建的内容获得唯一性的语法是什么?
答案 0 :(得分:0)
尝试以下操作:
SELECT C.name, C.phone_id, C.created_at
FROM Contacts C, (SELECT phone_id, MAX(created_at) as created_at
FROM Contacts
GROUP BY phone_id) as C2
WHERE C.phone_id = C2.phone_id and C.created_at = C2.created_at
ORDER BY phone_id