select 具有航程补丁更改日期的唯一记录

select unique records with dates where voyage patch changes

我有一个 table,其中包含如下数据(简化)

+-----------+----------------------------+----------------------------+--------------+----------------+
| voyage_id | voyage_start_at            | voyage_end_at              | dest_city_id | depart_city_id |
+-----------+----------------------------+----------------------------+--------------+----------------+
|         3 | 2020-01-06 09:00:00.000000 | 2020-01-07 09:00:00.000000 |        21761 |           1778 |
|         2 | 2020-01-05 09:00:00.000000 | 2020-01-06 09:00:00.000000 |         1778 |           1680 |
|         2 | 2020-01-04 09:00:00.000000 | 2020-01-05 09:00:00.000000 |         1778 |           1680 |
|         2 | 2020-01-03 09:00:00.000000 | 2020-01-04 09:00:00.000000 |         1778 |           1680 |
|         2 | 2020-01-02 09:00:00.000000 | 2020-01-03 09:00:00.000000 |         1778 |           1680 |
|         1 | 2020-01-01 09:00:00.000000 | 2020-01-02 09:00:00.000000 |         1680 |           1677 |
|         1 | 2020-01-01 00:00:00.000000 | 2020-01-01 09:00:00.000000 |         1680 |           1677 |
+-----------+----------------------------+----------------------------+--------------+----------------+

重要的事情是:

  1. voyage_start_at日期与上次航次相同voyage_end_at
  2. voyage_id每行相同,目的地和出发城市相同(航程可以拆分成几行

我想要得到的是从一个城市到另一个城市的航程列表,其中日期从第一个航程开始 voyage_start_at,从最后一个航程开始 voyage_end_at,所以这个例子应该是这样的:

+-----------+----------------------------+----------------------------+--------------+----------------+
| voyage_id | voyage_start_at            | voyage_end_at              | dest_city_id | depart_city_id |
+-----------+----------------------------+----------------------------+--------------+----------------+
|         3 | 2020-01-06 09:00:00.000000 | 2020-01-07 09:00:00.000000 |        21761 |           1778 |
|         2 | 2020-01-02 09:00:00.000000 | 2020-01-06 09:00:00.000000 |         1778 |           1680 |
|         1 | 2020-01-01 00:00:00.000000 | 2020-01-02 09:00:00.000000 |         1680 |           1677 |
+-----------+----------------------------+----------------------------+--------------+----------------+

我相信这可以通过 MySQL 查询获得,所以我试图通过使用 DISTINCTJOIN 和子查询来获得它,但我没有得到它根本。现在我只是想知道是否可以通过 SQL 来完成,或者我应该从 db 获取原始数据并根据我的需要转换它们(我正在使用 PHP 和 TWIG)。

任何有用的提示和想法将不胜感激。

你可以在这里使用聚合:

select
    voyage_id,
    min(voyage_start_at) voyage_start_at,
    max(voyage_end_at) voyage_end_at,
    dest_city_id,
    depart_city_id 
from mytable
group by voyage_id, dest_city_id, depart_city_id 

Demo on DB Fidddle:

voyage_id | voyage_start_at     | voyage_end_at       | dest_city_id | depart_city_id
--------: | :------------------ | :------------------ | -----------: | -------------:
        3 | 2020-01-06 09:00:00 | 2020-01-07 09:00:00 |        21761 |           1778
        2 | 2020-01-02 09:00:00 | 2020-01-06 09:00:00 |         1778 |           1680
        1 | 2020-01-01 00:00:00 | 2020-01-02 09:00:00 |         1680 |           1677