[英]select from table with group by where date>date in mysql table
我需要的是,為 machine_id 和 operation_id 的唯一組合顯示第一個 done_on_date,這將使用 group by function 完成,我想再次顯示 done_on_date,它必須在第一個選擇 done_on_date 之后為 machine_id 和操作 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 第二次完成是 15/04/2017。 對於 porsche_1,Service_1 第三次完成是 18/04/2017,對於 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
因此,其他汽車也是如此。
希望現在已經足夠清楚了。
您似乎需要每種汽車和服務類型的所有服務日期。 由於您沒有包含表的架構,因此我僅使用您提到的列來編寫此文件。 隨意添加任何缺失的列。 我還創建了一個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`;
結果:
| 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 |
編輯
這應該會產生您的示例輸出,對於每輛汽車和服務以及日期,它將輸出汽車、服務、日期和下一個服務日期:
查詢 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`
結果:
| 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 | |
源數據:
| 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 |
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.