根据“新记录”更新表'上一条记录'

时间:2015-10-30 06:53:27

标签: sql

是否可以根据 machine_id 使用新行 date_start 更新上一行 date_end

机器日志:

<script type="text/javascript">

  function enableTextBox(regularId, urgentId, btnId) 
  {
    //alert(regularId + " " + urgentId + " " + btnId);
    document.getElementById(regularId).removeAttribute("disabled");
    document.getElementById(urgentId).removeAttribute("disabled");
    //document.getElementById(regularId).className = "form-control";
    //document.getElementById(urgentId).className = "form-control";
    var btnSpan = document.getElementById(btnId);
    while(btnSpan.firstChild)
    {
      btnSpan.removeChild(btnSpan.firstChild);
    }
    var btn = document.createElement("INPUT");
    btn.type = "button";
    btn.value = "Save";
    btn.className = "btn btn-primary";
    btn.id = btnId;
    btn.onclick = function(){ saveRates(this.id, document.getElementById(regularId).value, document.getElementById(urgentId).value)};
    btnSpan.appendChild(btn);
  }

  function saveRates(btnId, regularValue, urgentValue)
  {
    //alert(btnId + " " + regularValue + " " + urgentValue);
    var clothType = btnId.substring(0, btnId.indexOf("-"));
    //alert(clothType);
    var tariffCardType = btnId.substring(btnId.indexOf("-")+1, btnId.indexOf("-", btnId.indexOf("-")+1));
    //alert(tariffCardType);
    var jobTypeCode = btnId.substring(btnId.indexOf("-", btnId.indexOf("-")+1)+1, btnId.length);
    if(jobTypeCode == "I")
    {
      jobType = "iron";
    }
    if(jobTypeCode == "WI")
    {
      jobType = "washiron";
    }
    if(jobTypeCode == "D")
    {
      jobType = "drycleaning";
    }
    //alert(jobType);

    var newRegularRate = regularValue;
    var newUrgentRate = urgentValue;

    $.ajax
    (
      {
        url:'/simplerAP/index.php?r=site/UpdateRates',
        type:'post',
        data:{tarifftardtype:tariffCardType, jobtype:jobType, clothtype:clothType, newregularrate:newRegularRate, newurgentrate:newUrgentRate},
        success:function(data)
                {
                  document.getElementById('demo').innerHTML = data;
                },
        cache:false}
    );
  }


  </script>

2 个答案:

答案 0 :(得分:1)

如果您使用的是SQL Server 2012或更高版本,则可以使用LEAD功能: SQL Fiddle

UPDATE m
SET m.date_end = lead.endDate
FROM machine_log AS m
INNER JOIN 
(
SELECT 
record_id
, LEAD(date_start) OVER (PARTITION BY machine_id ORDER BY date_start) endDate
FROM machine_log
) AS lead ON m.record_id = lead.record_id

如果您使用的是MySQL,可以使用Limit来模拟,如下所示: SQL Fiddle

UPDATE machine_log m
INNER JOIN 
(select 
 record_id, 
 (select b.date_start
  from machine_log b
  where b.machine_id=a.machine_id
    and b.date_start > a.date_start
  order by b.date_start Limit 1) as endDate
  from machine_log a
  order by record_id,date_start) lead
ON m.record_id = lead.record_id
SET m.date_end = lead.endDate
;

答案 1 :(得分:0)

如果您使用SQL Server:

covpatt=[1 0 2;0 0 0;-3 0 2]

covpatt =

 1     0     2
 0     0     0
-3     0     2

%your code segment

covpatt1=covpatt;
covpatt(all(~covpatt1,2),:)=[];
covpatt(:,all(~covpatt1,1))=[];

%result

covpatt =

 1     2
-3     2