选择所有并按列A排序,不包含列B的副本,然后按列C排序

时间:2017-02-19 07:10:12

标签: mysql

如何选择行中的所有列,按最新updated列排序,而不重复phone列,然后按points列对其进行排序?

+----+----------+---------+------------+
| id | phone    | points  | updated    |
+----+----------+---------+------------+
| 1  | iPhone 5 | 123156  | 2017-02-01 |
| 2  | iPhone 5 | 6512    | 2017-02-02 |
| 3  | iPhone 4 | 165489  | 2017-02-03 |
| 4  | iPhone 4 | 135416  | 2017-02-05 |
| 5  | iPhone 6 | 65459   | 2017-02-19 |
+----+----------+---------+------------+

获得如下结果:

+----+----------+---------+------------+
| id | phone    | points  | updated    |
+----+----------+---------+------------+
| 4  | iPhone 4 | 135416  | 2017-02-05 |
| 5  | iPhone 6 | 65459   | 2017-02-19 |
| 2  | iPhone 5 | 6512    | 2017-02-02 |
+----+----------+---------+------------+

3 个答案:

答案 0 :(得分:0)

试试这个

   SELECT
         p1.id,p1.phone,p1.points,p2.updated 
   FROM your_table p1
   JOIN (
        SELECT MAX(updated) as updated 
   FROM your_table GROUP BY phone)p2
   ON p2.updated = p1.updated 
   ORDER BY p1.points desc;

答案 1 :(得分:0)

试试这个:

SELECT pe.*
FROM phoneEntry pe
INNER JOIN 
(SELECT phone, MAX(updated) AS latestUpdate
    FROM phoneEntry
    GROUP BY phone) phoneGrouped 
ON pe.phone = phoneGrouped.phone 
AND pe.updated = phoneGrouped.latestUpdate
ORDER by points desc;

答案 2 :(得分:0)

尝试这个,

SELECT DISTINCT * from (SELECT DISTINCT * FROM (select * from t1 ORDER BY updated DESC) temp ORDER BY temp.updated DESC) tt GROUP BY phone ORDER by points DESC