dream518518518 2016-09-04 09:30
浏览 41
已采纳

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

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. 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. 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.

Not good this explaination either so here the 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. The query looks good so i have really no idea.

  • 写回答

1条回答 默认 最新

  • douwei7501 2016-09-04 09:35
    关注

    You don't need to calculate this value yourself, just use count(), sum() and group by, e.g.

    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.

    With mysqli, taken from the online manual of mysqli_fetch_row, something along these lines (untested)

    $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>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答