4

我想通过或或无论如何选择不同的customer_id顺序s.notime

s.no     customer_id         time
1        3                   100001
2        2                   100002
3        4                   100003
4        3                   100004
5        2                   100005

我在用

select distinct(customer_id) from table_name order by time DESC

它给出了答案,4 2 3但正如我想要的那样2 3 4

4

3 回答 3

16

所以你的问题陈述是“你想要customer_id的列表,从最大的时间值降序排序”,对吗?

SELECT customer_id, MAX(time)
FROM table_name
GROUP BY customer_id
ORDER BY MAX(time) DESC
于 2013-10-12T12:30:33.450 回答
0

您必须像这样进行第二次选择:

SELECT
    `number`
FROM
(
    SELECT
        DISTINCT(number) AS number
    FROM
        `table_name`
    ORDER BY
        `time` DESC
) AS `temp`
ORDER BY
    `number` ASC
于 2013-10-12T12:28:33.073 回答
0

可以使用第二个查询来完成

$query = "SELECT * 
            FROM (SELECT customer_id
                FROM table_name                 
                ORDER BY customer_id DESC) as rows              
            GROUP BY customer_id";
于 2016-11-25T15:35:46.883 回答