gpt4 book ai didi

mysql - SELECT all 并按 A 列排序,不重复 B 列,然后按 C 列排序

转载 作者:行者123 更新时间:2023-11-29 19:23:04 26 4
gpt4 key购买 nike

如何选择一行中的所有列按最新的更新列排序,不重复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 |
+----+----------+---------+------------+

最佳答案

试试这个

   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;

关于mysql - SELECT all 并按 A 列排序,不重复 B 列,然后按 C 列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42324633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com