Query to get quarterly data

1

Very good, I have a query that is generated by making a selection by means of a <select> . In the <select> you have to choose the month and the year, and send the variables to a query where you extract the data for that particular month. It works correctly!

The problem is that I would like to do the same but obtain the data of 1 quarter. The year has 4 quarters, so in my <select> there will be 4 options: "1 Quarter", "2 Quarter", etc ...

In the code I check the $trim to assign a range of dates so that later I can check it. But it does not work for me.

I update the code according to the help received:

if (isset($_POST['enviar'])) {
    $trim=$_POST['trim'];
    $anno=$_POST['ano'];

      if ($trim == '1'){
          $FechaInicio = $anno."-01-01";
          $FechaFin = $anno."-04-01";
     } 
}

$sql_a=mysqli_query($con, "SELECT * FROM facturas,clientes 
                           WHERE facturas.id_cliente=clientes.id_cliente 
                           AND estado_factura=2 
                           AND facturas.fecha_factura BETWEEN '$FechaInicio' AND '$FechaFin'");

Thank you very much!

    
asked by Jok 08.06.2018 в 10:25
source

2 answers

1

A quarter is three months between two dates (January 1 of that year until April 1, for example).

You could use the Between operator to select the data after one date and previous to another.

SELECT *
FROM facturas,clientes
WHERE facturas.id_cliente=clientes.id_cliente 
  AND estado_factura=2
  AND fecha_factura BETWEEN ('$fechaInicio' AND '$fechaFin')
ORDER BY facturas.fecha_factura ASC;

$FechaInicio and $FechaFin would be your quarter delimiters.

    
answered by 08.06.2018 / 10:37
source
0

You can ask if the month is in the period you want. Example:

$trim1 = array(1,2,3);
$trimestre = implode(",", $trim1);

"SELECT * FROM facturas,clientes WHERE facturas.id_cliente=clientes.id_cliente AND estado_factura=2 AND MONTH(fecha_factura) IN ($trimestre) AND YEAR(fecha_factura) = '$anno' ORDER BY facturas.fecha_factura ASC"
    
answered by 08.06.2018 в 10:53