如何按最新日期排序数据,然后按不同ID排序

时间:2019-04-09 20:19:27

标签: mysql

我在mysql表中有此数据:

ID_Registro  Tiempo              Tipo ID_Guardia ID_Estacion
+---------+---------------------+----+---------+------+
| 18      | 2018-07-15 12:00:00 | 2  |    3    | 7    |
| 17      | 2018-07-15 12:00:00 | 2  |    3    | 7    |
|  7      | 2018-07-15 11:00:00 | 1  |    4    | 7    |
|  6      | 2018-07-15 10:00:00 | 1  |    3    | 7    |
+---------+---------------------+----+---------+------+

我通过以下查询获取此数据:

    SELECT ID_Registro,
       Tiempo,
       Tipo,
       ID_Guardia,
       ID_Estacion
FROM registro_guardias 
WHERE ID_Estacion = 7 
ORDER BY Tiempo DESC, ID_Registro DESC

我需要获取的是最新的注册表(在日期时间字段Tiempo上,并且我也要通过ID对其进行排序),但要使用每个不同的Guards(ID_Guardia)

含义:

ID_Registro  Tiempo             Tipo ID_Guardia ID_Estacion
+---------+---------------------+----+---------+------+
| 18      | 2018-07-15 12:00:00 | 2  |    3    | 7    |
|  7      | 2018-07-15 11:00:00 | 1  |    4    | 7    |
+---------+---------------------+----+---------+------+

该工作站上的所有ID_Guardia(ID_Estacion)都不同。


到目前为止,我已经尝试放置GROUP BY:

SELECT ID_Registro,
       Tiempo,
       Tipo,
       ID_Guardia,
       ID_Estacion
FROM registro_guardias 
WHERE ID_Estacion = 7 
GROUP BY ID_Guardia
ORDER BY Tiempo DESC, ID_Registro DESC

获取:

    ID_Registro  Tiempo              Tipo  ID_Guardia ID_Estacion
    +---------+---------------------+----+---------+------+
    |  7      | 2018-07-15 11:00:00 | 1  |    4    | 7    |
    |  6      | 2018-07-15 10:00:00 | 1  |    3    | 7    |
    +---------+---------------------+----+---------+------+

放置DISTINCT:

SELECT DISTINCT ID_Guardia,
       ID_Registro,
       Tiempo,
       Tipo,
       ID_Estacion
FROM registro_guardias rg
WHERE ID_Estacion = 7 
ORDER BY Tiempo DESC, ID_Registro DESC


ID_Guardia  ID_Registro  Tiempo              Tipo ID_Estacion
+----------+-----------+---------------------+---+---+
| 3        | 18        | 2018-07-15 12:00:00 | 2 | 7 |
| 3        | 17        | 2018-07-15 12:00:00 | 2 | 7 |
| 4        |  7        | 2018-07-15 11:00:00 | 1 | 7 |
| 3        |  6        | 2018-07-15 10:00:00 | 1 | 7 |
+----------+-----------+---------------------+---+---+

我可能在想错了,但是如何获得所需的结果?

更新:

我已经尝试过这种方式,但是我仍然没有获得查询中同样需要的主要ID。

  SELECT ID_Registro,
       MAX(Tiempo),
       Tipo,
       ID_Estacion
       ID_Guardia,
FROM registro_guardias 
WHERE ID_Estacion = 7
GROUP BY ID_Guardia
ORDER BY Tiempo ASC

我明白了,ID的DONT与最高(Tiempo)时间注册表匹配。它们只匹配第一个匹配项。

+---+---------------------+---+---+---+
| 1 | 2018-07-15 13:00:00 | 2 | 7 | 3 |
| 2 | 2018-07-15 13:00:00 | 2 | 7 | 4 |
| 7 | 2018-07-15 11:00:00 | 2 | 7 | 5 |
| 8 | 2018-07-15 13:00:00 | 2 | 7 | 6 |
+---+---------------------+---+---+---+

1 个答案:

答案 0 :(得分:0)

我认为您想按ID_Guardia分组并选择具有最大Tiempo值的行。那将是每组最多n个,因此可能的解决方案之一是这样的:

SELECT r1.ID_Registro,
   r1.Tiempo,
   r1.Tipo,
   r1.ID_Guardia,
   r1.ID_Estacion
FROM registro_guardias r1 LEFT JOIN registro_guardias r2
  ON (r1.ID_Guardia = r2.ID_Guardia AND r1.Tiempo < r2.Tiempo)
WHERE r2.Tiempo IS NULL AND ID_Estaction = 7;

有关其他解决方案,请参见https://stackoverflow.com/questions/tagged/greatest-n-per-group