【问题标题】:How to combine order by and group by together? condition : order by one column group by another column如何将 order by 和 group by 结合在一起?条件:按一列按另一列分组
【发布时间】:2019-08-07 04:46:41
【问题描述】:

我对 MySQL 中的 order by 和 group by 组合有一个问题。 以下是我的表结构。

+-------------+-----------+-----------+----------------------------+
| driver_id   | lat       | lng       | creation                   |
+-------------+-----------+-----------+----------------------------+
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-13 09:21:39.225873 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 12:30:09.608296 |
| TDRV-000021 | 18.659135 | 73.756445 | 2019-03-10 15:38:37.954508 |

当我使用以下 SQL 命令(Order by Creation desc)时,

select driver_id, lat, lng, creation from `tabReal Time Location` order by creation desc;

我得到了以下结果,

+-------------+-----------+-----------+----------------------------+
| driver_id   | lat       | lng       | creation                   |
+-------------+-----------+-----------+----------------------------+
| TDRV-000021 | 20.659180 | 73.756480 | 2019-03-16 08:13:41.155528 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 19:44:06.206690 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 19:36:25.571260 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 19:35:55.115124 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 19:35:46.281953 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 19:35:00.986849 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 19:33:38.370099 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 19:32:57.589272 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 19:30:24.582391 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-15 19:29:23.809564 |

当我使用 order by 和 group by 组合时,

select driver_id, lat, lng, creation from `tabReal Time Location` group by driver_id order by creation desc;

+-------------+-----------+-----------+----------------------------+
| driver_id   | lat       | lng       | creation                   |
+-------------+-----------+-----------+----------------------------+
| TDRV-000027 | 18.659180 | 73.756480 | 2019-03-15 13:47:37.338828 |
| TDRV-000021 | 18.659180 | 73.756480 | 2019-03-13 09:21:39.225873 |
+-------------+-----------+-----------+----------------------------+

但正如您在上面的结果中看到的那样,我得到了错误的结果,我期待 latest updated lat, lng 列如下所示。

+-------------+-----------+-----------+----------------------------+
| driver_id   | lat       | lng       | creation                   |
+-------------+-----------+-----------+----------------------------+
| TDRV-000021 | 20.659180 | 73.756480 | 2019-03-16 08:13:41.155528 |
| TDRV-000027 | 20.659180 | 90.756480 | 2019-03-15 14:07:41.543089 |
+-------------+-----------+-----------+----------------------------+

【问题讨论】:

  • 没有聚合的分组不太可能产生有用的结果。你可以谷歌最近的 mysql。

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


【解决方案1】:

这是期待最新更新的一种方法

select t.driver_id, lat, lng, order_id  from `tabReal Time Location` t INNER JOIN  (select max(creation) as latest_date,driver_id  FROM `tabReal Time Location` group by  driver_id)d ON t.creation= d.latest_date and d.driver_id = t.driver_id;

【讨论】:

  • 你为什么使用转换功能?
  • 加入日期列将是首选,而不是日期时间或时间戳列
猜你喜欢
  • 2012-04-19
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 2011-06-28
  • 1970-01-01
相关资源
最近更新 更多