按组排序

时间:2011-06-28 14:42:34

标签: mysql group-by sql-order-by

是否有一种有效的方式来执行订单和分组,以便从每个组(从订单中)获取特定项目。

//This is the best way i have come up with.
SELECT D.*
FROM (
    SELECT *
    FROM devices
    ORDER BY time
) AS D
GROUP BY D.location

2 个答案:

答案 0 :(得分:2)

这对我有用

SELECT *
FROM (SELECT *
      FROM col
      ORDER BY col DESC,date DESC)a
GROUP BY col

答案 1 :(得分:1)

SELECT *
FROM devices
GROUP BY D.location
ORDER BY time --ASC or DESC default is ASC i believe

无需子查询