Group data related to a column

Asked

Viewed 263 times

0

I’m having trouble grouping times by date.

I have an html table that is populated from an sql query. The idea would be to list in one column the date and in the other column all times referring to that date without having to skip line as in the photo I posted at the end. What could be done?

Code:

<table class="table table-striped">
   <thead>  
      <tr>
    <th>Data</th>
    <th>Marcações</th>          
      </tr>
  </thead>   
<?php

$con = mysql_connect($host, $user, $pass) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db($db, $con);

$sql= mysql_query("SELECT dia, hora FROM marcacoes");
    while($exibe = mysql_fetch_assoc($sql)){

    $tabela ='<tbody>'; 
    $tabela .='<tr>';
    $tabela .='<td>'.date("d-m-Y", strtotime($exibe['dia'])).'</td>'; 
    $tabela .='<td>'.$exibe['hora'].'</td>';
    $tabela .='</tr>';
    $tabela .='</tbody>';

    echo $tabela;

    }   
?>

Example of the problem:

inserir a descrição da imagem aqui

1 answer

1


An example in Mysql would be GROUP_CONCAT that concatenates a column, with value in N rows in a single column and a row, provided that the GROUP BY clause is used in the other non-cracker columns, as an example below:

SELECT data, GROUP_CONCAT(hora) As horarios FROM `marcacoes` GROUP BY data;

In this case you would retrieve the values of the 'date' and 'time' columns in your code to display the information returned by SELECT.

I hope I’ve helped.

  • Dude worked out!! Right there in case I display this consultation?

  • To display is a normal query, you must name GROUP_CONCAT with some alias to facilitate recovery of values. I will edit my reply with an example.

  • Perfect!! Thanks Bruno for the answer helped me a lot!

Browser other questions tagged

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