简体   繁体   English

PHP - 嵌套的递归SQL查询不检索值

[英]PHP - nested recursive SQL query doesn't retrieve values

What i'm trying to do is to print a table and in each row print, for each different value in the DB, the value, the number of the rows in the DB with that value and the sum of the values. 我要做的是打印一个表,并在每一行打印,为DB中的每个不同的值,值,DB中具有该值的行数和值的总和。 Sorry this is not well explayned, more easy go straight to the code 对不起,这个问题没有得到很好的解释,更容易直接进入代码

$query_voucher1="SELECT * FROM voucher WHERE consegnato= 0 AND IDprestazione=0 ORDER BY codice";
$risultato_query_voucher1=@mysql_query($query_voucher1);
echo "<table class='table table-striped' style='margin-top:70px; width: 60%;'>
      <caption>Riassunto Voucher Liberi</caption>
       <tr>
         <th>Codice di controllo</th>
         <th>Quantità (solo liberi)</th>
         <th>Data Emissione</th>
         <th>Valore Pacchetto (solo liberi)</th>
        </tr>";

$prevcodice= NULL;
$curcodice= 10;
while ($row_voucher1=mysql_fetch_row($risultato_query_voucher1)) {
                if ($curcodice!=$prevcodice){
                  $array_temp_query_value=array();
                  if ($modifica==true){
                  $temp_query_value=mysql_query("SELECT valorelordo FROM voucher WHERE codice='$row_voucher1[2]' AND consegnato=0 ");
                  }else{
                    $temp_query_value=mysql_query("SELECT valorelordo FROM voucher WHERE codice='$row_voucher1[2]' AND consegnato=0 AND IDprestazione=0");
                  }
                  while(mysql_fetch_row($temp_query_value)){
                    array_push($array_temp_query_value, $temp_query_value[0]);
                  }
                  echo "<tr>
                          <td>" . $row_voucher1[2] . "</td>
                          <td>" . count($array_temp_query_value) . "</td>
                          <td>" . print_r($array_temp_query_value). "</td>
                        </tr>";
                  $prevcodice=$row_voucher1[2];

                }
              $curcodice=$row_voucher1[2];
              }

              echo "</table>";

So let's say i have a DB with the field codice and the many values in this fields are repeated. 因此,假设我有一个带有字段codice的数据库,并且重复了这个字段中的许多值。 What i wanna do is take print once the value, once the number of rows where codice is the same and once the sum of the values where codice is the same. 我要做的就是把打印一次值,一旦行数,其中codice是相同的,一旦当值的总和codice是一样的。

Not good this explaination either so here the JSFiddle . 这个解释不是很好,所以这里是JSFiddle

The $array_temp_query_value returns the correct number of values so i can count them to fill the second field but all the values in the array are empty so i can't sum them. $array_temp_query_value返回正确的值数,因此我可以将它们计算为填充第二个字段,但是数组中的所有值都是空的,所以我无法对它们求和。 The query looks good so i have really no idea. 查询看起来不错,所以我真的不知道。

You don't need to calculate this value yourself, just use count() , sum() and group by , eg 您不需要自己计算这个值,只需使用count()sum()group by ,例如

select codice, count(*), sum(codice)
from voucher
group by codice

This gives you all existing values in your table, the number of rows of each value and the sums. 这将为您提供表中的所有现有值,每个值的行数和总和。


Upfront, don't use mysql_* functions, because they are deprecated and removed in PHP 7. mysql_* ,不要使用mysql_*函数,因为它们在PHP 7中弃用和删除。

With mysqli , taken from the online manual of mysqli_fetch_row , something along these lines (untested) 使用mysqli ,取自mysqli_fetch_row的在线手册,这些内容(未经测试)

$result = mysqli_query($conn, 'select codice, count(*), sum(codice) from voucher group by codice');
while ($row = mysqli_fetch_row($result)) {
    echo '<tr><td>' . $row[0] . '</td><td>' . $row[1] . '</td><td>' . $row[2] . '</td></tr>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM