【发布时间】: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,示例数据通常是无意义的。