MySQL and PHP clustering

3

I need to group a query with a detail, grouping should be done only when the information is repeated in sequence, for example:

id     nome
1      lucas
2      lucas
3      marcos
4      lucas
5      marcos

In the example above, only the "lucas" with ID's 1 and 2 can be grouped, the other information not, since they are not in sequence.

The following is an excerpt from the query:

$SQL = "select * from clientes where id = '".$id."' group by nome order by id desc ";
$RSS = mysql_query($SQL, $conexao);
while($RS = mysql_fetch_array($RSS)){
  echo $RS["nome"]."<br>";
}
    
asked by anonymous 12.01.2018 / 16:56

1 answer

2

Instead of using SQL GROUP BY, let's join the names in sequence in PHP code.

It would look like this:

$SQL = "SELECT * FROM clientes ORDER BY id";
$RSS = mysql_query($SQL, $conexao);
while($RS = mysql_fetch_array($RSS)){
  if ($RS["nome"] != $nome_atual)
    echo $RS["nome"]."<br>";
  $nome_atual = $RS["nome"];
}

Without the MySQL 'GROUP BY' clause, the names will follow the informed sequence. 'If' checks if the name received from the bank is different from the previous one. If it is, it publishes.

    
12.01.2018 / 20:29