【问题标题】:select from table with group by where date>date in mysql table在 mysql 表中按日期 > 日期从分组表中选择
【发布时间】:2018-04-28 12:57:55
【问题描述】:

我需要的是,为 machine_id 和 operation_id 的唯一组合显示第一个 done_on_date,这将使用按功能分组完成,我想再次显示 done_on_date,它必须在第一个选择的 done_on_date 之后完全是 done_on_date 用于唯一组合machine_id 和 operation_id。

到目前为止,我尝试过的内容如下所示。

$query = "SELECT * 
    FROM maintenance_entry_table 
    LEFT JOIN (
        SELECT (done_on_date) AS done_on_date1, maintenance_entry_table.*
        FROM maintenance_entry_table $where 
        GROUP BY machine_id,operation_id) as groupedDate 
    ON maintenance_entry_table.machine_id = groupedDate.machine_id
        AND maintenance_entry_table.operation_id = groupedDate.operation_id 
        AND maintenance_entry_table.done_on_date > groupedDate.done_on_date1 
    group by machine_id, operation_id”;

$where 被明确定义为其他地方的 where 子句。

但它似乎不起作用。对于 machine_id 和 operation_id 的唯一组合,它会显示相同的 done_on_date 而不是之后的那个。我完全不知道。

它在 html 页面上的显示方式如下所示。

echo "<tr>";
echo "<td>" . $row['machine_id'] ."</td>"; 
echo "<td>" . $row['operation_id'] ."</td>"; 
echo "<td>" . $row['done_on_date'] ."</td>"; 
echo "<td>" . $row['done_on_date1'] ."</td>"; //this brings up same date as 'done_on_date' whereas it should be the next date coming in row for the unique combination of machine_id and operation_id
echo "</tr>";

如果我进一步解释,它类似于,例如,您有 2 辆保时捷汽车,分别定义为 porsche_1 和 porsche_2。现在,porsche_1 在 1 年内送修工维修 4 次。现在让我们假设执行的服务机制类型相同,称为 service_1。因此,对于 porsche_1,Service_1 第一次完成是 01/04/2017。对于 porsche_1,Service_1 第二次完成是 2017 年 4 月 15 日。对于 porsche_1,Service_1 第三次完成是 2017 年 4 月 18 日,对于 porsche_1,Service_1 第四次完成是 20/04/2017。我希望将这些详细信息放在一行中,如下所示,

car_name    service_name    done_date     done_date  
porsche_1     service_1,     01/04/2017    15/04/2017
porsche_1     service_1      15/04/2017    18/04/2017
porsche_1     service_1      18/04/2017    20/04/2018
porsche_1     service_1      20/04/2017   blank  //bcoz service not yet executed

其他汽车也是如此。

希望现在已经足够清楚了。

【问题讨论】:

  • SELECT * ... group by machine_id, operation_id 要求提供无效数据,因为您滥用 MySQL 的 GROUP BY 功能。阅读psce.com/en/blog/2012/05/15/…
  • @SumanDey 如何构建查询以获得 done_on_date 和另一个 done_on_date 以获得 machine_id 和 operation_id 的唯一组合。最后,汽车的解释是不言自明的。
  • 我的意思是它实际上是混淆 AF
  • 因此,您是在尝试使垂直表相对于完成日期水平放置,还是每个服务都专门有两个名称无意义的完成日期?
  • @MasonStedman,示例数据通常是无意义的。

标签: php jquery html mysql


【解决方案1】:

您似乎需要每种汽车和服务类型的所有服务日期。由于您没有包含表的架构, 我只用你提到的专栏写了这篇文章。随意添加任何缺少的列。我还创建了一个SQLFiddle 构建表并运行下面的查询。

日期不是单独的列,而是在一列中,以逗号分隔。您可以在显示 价值观。如果为每个日期创建一个单独的列,则必须知道最大日期数并为每个日期构建一个包含连接的查询。

SELECT
    a.`car_name`,
    a.`service_name`,
    GROUP_CONCAT(a.`done_date` ORDER BY a.`done_date` ASC) as `Dates_Done`
FROM `maintenance_entry_table` a
LEFT JOIN (
    SELECT 
        c.`car_name`,
        c.`service_name`,
        MIN(`done_date`) as `first_date`
    FROM `maintenance_entry_table` c
    GROUP BY c.`car_name`,c.`service_name`
    ) b
ON b.`first_date` < a.`done_date` AND b.`car_name` = a.`car_name` AND b.`service_name` = a.`service_name`
GROUP BY a.`car_name`,a.`service_name`
ORDER BY a.`car_name`,a.`service_name`;

Results

|  car_name |   service_name |                       Dates_Done |
|-----------|----------------|----------------------------------|
|    benz_1 |     oil_change | 2017-01-14,2017-05-24            |
| porsche_1 |     oil_change | 2017-01-04,2017-04-15,2017-07-12 |
| porsche_1 | replace_wipers | 2017-01-04                       |
| porsche_1 |     tire_check | 2017-01-04,2017-06-11            |
| porsche_2 |     oil_change | 2017-05-01,2017-08-20            |

编辑


这应该产生您的示例输出,对于每辆汽车和服务和日期,它将输出汽车、服务、日期和下一个服务日期:

SQLFiddle

查询 2

SELECT
    a.`car_name`,
    a.`service_name`,
    a.`done_date`,
    coalesce(MIN(b.`done_date`),'') as `next_done_date`
FROM `maintenance_entry_table` a
LEFT JOIN `maintenance_entry_table` b
ON b.`done_date` > a.`done_date` AND 
    b.`car_name` = a.`car_name` AND 
    b.`service_name` = a.`service_name`
GROUP BY a.`car_name`,a.`service_name`,a.`done_date`
ORDER BY a.`car_name`,a.`service_name`,a.`done_date`

Results

|  car_name |   service_name |  done_date | next_done_date |
|-----------|----------------|------------|----------------|
|    benz_1 |     oil_change | 2017-01-14 |     2017-05-24 |
|    benz_1 |     oil_change | 2017-05-24 |                |
| porsche_1 |     oil_change | 2017-01-04 |     2017-04-15 |
| porsche_1 |     oil_change | 2017-04-15 |     2017-07-12 |
| porsche_1 |     oil_change | 2017-07-12 |                |
| porsche_1 | replace_wipers | 2017-01-04 |                |
| porsche_1 |     tire_check | 2017-01-04 |     2017-06-11 |
| porsche_1 |     tire_check | 2017-06-11 |                |
| porsche_2 |     oil_change | 2017-05-01 |     2017-08-20 |
| porsche_2 |     oil_change | 2017-08-20 |                |

Source Data

| id |  car_name |   service_name |  done_date |
|----|-----------|----------------|------------|
|  1 | porsche_1 |     oil_change | 2017-01-04 |
|  2 | porsche_1 |     oil_change | 2017-04-15 |
|  3 | porsche_1 |     oil_change | 2017-07-12 |
|  4 |    benz_1 |     oil_change | 2017-01-14 |
|  5 |    benz_1 |     oil_change | 2017-05-24 |
|  6 | porsche_1 |     tire_check | 2017-01-04 |
|  7 | porsche_1 |     tire_check | 2017-06-11 |
|  8 | porsche_1 | replace_wipers | 2017-01-04 |
|  9 | porsche_2 |     oil_change | 2017-05-01 |
| 10 | porsche_2 |     oil_change | 2017-08-20 |

【讨论】:

  • 我肯定会在明天尝试,但我担心 min(done_date) 将始终以最小日期开始每一行,而所需的正是汽车示例中所述,其中第一行首先 done_date 和第二个done_date..........第二个done_date 将成为第二行的第一个done_date,依此类推......对不起,不需要group by 我猜在左连接(选择语句)中,因为我会想要每辆车及其相关的服务类型。
  • 加入可防止您所说的同一辆车+服务重复每个日期作为第一个 done_date。除非那是你想要的。子查询中的 group by 是您如何获得特定汽车 + 服务的最早 done_date。如果您真的想在示例输出中显示,那么使用此结果可以在 PHP 中轻松完成。
  • 查看我的更新答案。如果您希望结果按 date_done 顺序排列,只需更改 ORDER BY 子句并将 a.date_done 放在首位。
  • 很抱歉打扰你,如果你能抽出一些时间,你能看看我正在努力解决的以下问题。 stackoverflow.com/questions/52531586/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
  • 2011-01-16
相关资源
最近更新 更多