Group a set of measures in PHP

Asked

Viewed 88 times

0

I’m having a problem uniting a set of values.

I have a wood marking system. I feed the database with the length x width of the wood

Id, id_request, length, width, date

Registros:

1, 3, 320, 20, 05-25-2017

2, 3, 320, 25, 05-25-2017

3, 3, 310, 10, 05-25-2017

4, 3, 310, 20, 05-25-2017

5, 3, 190, 15, 05-25-2017

Now I need a routine in PHP or Mysql that joins the results not to appear twice the same length and this way it joins the measures to be shown next to this length.

Example of how it would look:

320-20 x 25

310 - 10 x 20

190 - 15

How could I do that?

  • We should not use functions of the "mysql" extension because its development has been discontinued. If you add in Mysqli or PDO tags I publish the answer :)

  • Ready @Leocaracciolo

2 answers

1


Use the group_concat that concatenates a sequence of fields according to your defined query and group. group_concat is an aggregation function such as Count(), sum(), etc.

Mysqli

$servidor = "localhost";
$usuario = "USUARIO";
$senha = "SENHA";
$dbname = "NOME_DB";

$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);
    $query = ('SELECT comprimento, group_concat(largura SEPARATOR " * ") AS lista, SUM(largura) as largura_sum FROM arquivos GROUP BY comprimento');
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)){
    echo $row['comprimento']." - ";
    echo $row['lista']." Total: ";
    echo $row['largura_sum']."<br>";
}

PDO

$hostname="localhost";  
$username="USUARIO";  
$password="SENHA";  
$db = "NOME_DB";  
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);  
foreach($dbh->query('SELECT comprimento, group_concat(largura SEPARATOR " * ") AS lista, SUM(largura) as largura_sum FROM arquivos GROUP BY comprimento') as $row) {  

echo $row['comprimento'] . " - ";  
echo $row['lista']." Total: ";  
echo $row['largura_sum']."<br>"; 

} 

Generally, GROUP_CONCAT separates items with comma:

item1,item2,item3,item4

It is possible to define the separator to be used in this way:

GROUP_CONCAT(suacoluna SEPARATOR ' ')

Edited response to answer author’s comment asking for the sum of widths

  • So he returned this: comprimento lista&#xA;9.80 [BLOB - 4Bytes]&#xA;2.50 [BLOB - 4Bytes]&#xA;2.30 [BLOB - 4Bytes]&#xA;5.00 [BLOB - 18Bytes]

  • It worked! One more question. See if you can help me! Is it possible to sum these lengths directly in the query? For example if it has 3 results of pro widths same length, it sum these width (30+14+16 = 60)

  • THANK YOU! It worked perfectly. Thanks friend

1

You can concatenate the columns representing the dimensions and group the data:

Table:

create table test.pedidos
(
    id_pedido int auto_increment primary key,
    comprimento float null,
    largura float null,
    data date null
);

Some data:

INSERT INTO pedidos
  (comprimento, largura, data)
VALUES
  (320, 20, '2017-05-05'),
  (320, 25, '2017-05-05'),
  (310, 10, '2017-05-06'),
  (320, 25, '2017-05-06'),
  (320, 20, '2017-05-07'),
  (320, 20, '2017-05-08'),
  (300, 100, '2017-05-10')
;

And the consultation with the information you need:

SELECT
  concat(comprimento, '-', largura),
  count(1)
FROM pedidos
GROUP BY concat(comprimento, '-', largura);
  • The result I got was like this: concat(comprimento, '-', largura) count(1)&#xA;2.30-0.98 1&#xA;2.50-0.65 1&#xA;5.00-0.21 1&#xA;5.00-0.39 1&#xA;5.00-0.98 1&#xA;9.80-0.32 1&#xA; how do I handle this data? It keeps not grouping measures

Browser other questions tagged

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