从具有分组的表中选择日期>日期在mysql表

时间:2018-04-28 12:57:55

标签: php jquery html mysql

我需要的是,显示第一个done_on_date用于machine_id和operation_id的唯一组合,这将使用逐个功能完成,我想再次显示done_on_date,必须在第一个选择的done_on_date之后完全为done_on_date进行唯一组合of 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>";
&#13;
&#13;
&#13;

如果我进一步解释,例如,让我们说,你有2辆保时捷车被定义为porsche_1和porsche_2。现在,保时捷1被送到机修工,在1年内维修4次。现在让我们假设执行的服务机制类型与service_1相同。因此,Service_1第一次完成的时间是01/04/2017,适用于porsche_1。对于porsche_1,Service_1第二次完成时间为2017年4月15日。 Service_1第三次完成时间为2017年4月18日,对于porsche_1而言,Service_1第四次完成时间为2017年4月20日。我希望将这些细节放在如下所示的单行中,

&#13;
&#13;
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
&#13;
&#13;
&#13;

因此也适用于其他车型。

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

1 个答案:

答案 0 :(得分:1)

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

它们位于由逗号分隔的一列中,而不是日期的各列。显示时,您可以轻松地展开此列 值。如果为每个日期创建单独的列,则必须知道最大日期数,并为每个日期构建带有连接的查询。

   ui <- fluidPage(titlePanel("Demo Interface"),

            sidebarLayout(
              sidebarPanel(
                textInput("text1", "STATE:",value = " "),
                textInput("text2", "REVNUE:",value =" "),
                numericInput("text3", "RATE OF GROWTH:",value = " "),
                numericInput("text4", "STATES OWN TAX",value = " "),
                numericInput("text5", "REVENUE BUOYANCY",value = " "),
                actionButton("submit1", strong("Submit"))
              ), 
              mainPanel(
                verbatimTextOutput("txt1"),
                verbatimTextOutput("txt2"),
                verbatimTextOutput("txt3"),
                verbatimTextOutput("txt4"),
                verbatimTextOutput("txt5"),
                verbatimTextOutput("txt6")

              )
            )
          )

 server <- function(input,output){

  mdata <- eventReactive(input$submit1,{
   data <- c(input$text1,input$text2,input$text3,input$text4,input$text5)
   data[is.na(data)] <- ""
   data
  })

   output$txt1 <- renderText({paste("state name is as :", mdata()[1])})
   output$txt2 <-renderText({paste("revenue is as :",  mdata()[2])})
   output$txt3 <-renderText({ paste("rate of growth :",  mdata()[3])})
   output$txt4 <-renderText({paste("state own tax :",  mdata()[4])})
   output$txt5 <-renderText({paste("revenue buoyancy :",  mdata()[5])})
   }


    shinyApp(ui, server)
    it is the interface to enter the values from user and I want to save        the results in database sqlserver

<强> Results

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`;

修改

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

SQLFiddle

查询2

|  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            |

<强> Results

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`

<强> Source Data

|  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 |                |