Group values in a Mysql PHP query

Asked

Viewed 46 times

0

I am pulling values BD for a table with the results of the query, this search is by date, is working perfectly if I search for a single day, but I would like when to search for two or more days (ex 07/06/2018 to 08/06/2018), it brings the values grouped (compiled) the days of the query, today it brings the values skipping the lines and not grouping them;

code:

The sql:

$sql = "SELECT * FROM reporte_producao WHERE data_reporte BETWEEN '$data_ida' AND '$data_volta' AND turno = '$turno' AND grupo = '$v' ORDER BY id ASC";

to generate the table:

    foreach($num_array as $k => $v){

  ?>  
  <tr>
    <td><?php echo $num_array[$k]; ?></td>
    <td><?php echo $maq_array[$k]; ?></td>
    <td><?php echo $grupo_arr[$k]; ?></td>
    <td><?php echo $producao_array[$k]; ?></td>
    <td><?php echo (($producao_array[$k]) * 2); ?></td>
    <td><?php echo $segunda_array[$k]; ?></td>
    <td><?php echo $terceira_array[$k]; ?></td>
    <td><?php echo $producao_r_array[$k]; ?></td>
    <td><?php echo (($producao_r_array[$k])*2); ?></td>
    <td><?php echo $eficiencia_array[$k]."%"; ?></td>
  </tr>
  <?php } //fecha foreach

the most effective way to do this would be below? any suggestions?

$producao         += $data['producao'];

1 answer

0

You need to do group by by date:

SELECT
    *
FROM
    reporte_producao
WHERE
    data_reporte BETWEEN '$data_ida'
AND '$data_volta'
AND turno = '$turno'
AND grupo = '$v'
GROUP BY
    date_format(data_reporte, '%d/%m/%Y')
ORDER BY
    id ASC
  • Thanks for the feedback @Diego Souza, but could you explain what this line does? I know that GROUP BY groups, but what exactly this code is for?

  • It takes your date, transforms it into dd/mm/YYYY format to group. This is necessary because of the time your field probably has and is different. There would be no grouping.

  • nothing appears in the query;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.