Do an Update with ORDER BY in PHP

0

I have another problem at the moment of wanting to make the modification of the file, I have in the same table the years 2016 and 2017 with the regristers that start from 1 to ... x number, the problem is that the Id are repeated if the year begins, then I am trying to sort by date so that I take the most recent and so, the detail is that if I tell you to update me through IdExpediente = '". $ id, I modified the two years, then I've been thinking and I think the solution is to tell them to fit them per year and update me only those of 2017 and not those of last year, I present the code that I have been trying to modify but the syntax is where I fail, I do not know if they can guide me on how to go.

    $sql = "UPDATE tExpedientes 
  SET Descripcion='".$descripcion."', 
      Fecha='".$fecha."',
        IdEstatus='".$estado."',
      IdAutoridad='".$autoridad."',
      QuejosoNombre='".$quejosoNombre."',
      QuejosoApellidos='".$quejosoApellidos."',
      IdConceptoViolacion='".$conceptoViolacion."',
      ServidorPublico='".$servidorPublico."',
      TipoAutoridad='".$tipoAutoridad."',
      IdTipo='".$tipoExpediente."',
      IdTurnadoA='".$turnadoA."'
      WHERE IdExpediente='".$id "' ORDER BY fecha DESC, IdExpediente DESC"
    $version = sqlsrv_query($conn, $sql);
    
asked by Cesar Ortega 10.04.2017 в 17:18
source

2 answers

0

Well, I think you only need to add the condition of the year:

   $sql = "UPDATE tExpedientes 
 SET Descripcion='".$descripcion."', 
  Fecha='".$fecha."',
    IdEstatus='".$estado."',
  IdAutoridad='".$autoridad."',
  QuejosoNombre='".$quejosoNombre."',
  QuejosoApellidos='".$quejosoApellidos."',
  IdConceptoViolacion='".$conceptoViolacion."',
  ServidorPublico='".$servidorPublico."',
  TipoAutoridad='".$tipoAutoridad."',
  IdTipo='".$tipoExpediente."',
  IdTurnadoA='".$turnadoA."'
  WHERE IdExpediente='".$id "' AND fecha = '2017' ORDER BY fecha DESC, IdExpediente DESC"
$version = sqlsrv_query($conn, $sql);
    
answered by 10.04.2017 в 17:23
0

In the query you need to put in the condition of WHERE the years are the same:

$sql = "UPDATE tExpedientes 
SET Descripcion='".$descripcion."', 
    Fecha='".$fecha."',
    IdEstatus='".$estado."',
    IdAutoridad='".$autoridad."',
    QuejosoNombre='".$quejosoNombre."',
    QuejosoApellidos='".$quejosoApellidos."',
    IdConceptoViolacion='".$conceptoViolacion."',
    ServidorPublico='".$servidorPublico."',
    TipoAutoridad='".$tipoAutoridad."',
    IdTipo='".$tipoExpediente."',
    IdTurnadoA='".$turnadoA."'
WHERE IdExpediente='".$id "'
    AND year(Fecha) = year($fecha)
ORDER BY fecha DESC, IdExpediente DESC"
$version = sqlsrv_query($conn, $sql);

This will take the ids of the corresponding year

    
answered by 10.04.2017 в 23:54