简体   繁体   English

SQL - SELECT计算多个字段

[英]SQL - SELECT count multiple fields

I need to generate a conditional SQL query through PHP in which i sum and count some fields. 我需要通过PHP生成条件SQL查询,其中我总结并计算一些字段。 Let's cut it out and go directly to the query i've written. 让我们把它剪掉,直接进入我写的查询。

$query_voucher="SELECT *,
count(case WHEN IDprestazione=0 then 1 else null end) as quantitaliberi,
count(case WHEN IDprestazione != 0 AND pagato=0 then 1 else null end) as quantitaprenotati,
count(case WHEN pagato=1 then 1 else null end) as quantitapagati,
count(*) as quantita,
sum(valorelordo case WHEN IDprestazione=0 then 1 else 0 end) as valoreliberi,
sum(valorelordo case WHEN IDprestazione=1 AND pagato=0 then 1 else 0 end) as valoreprenotati,
sum(valorelordo case WHEN pagato=1 then 1 else 0 end) as valorepagati,
sum(*) as valore
FROM voucher";
$conditions=array();

if (!empty($IDpersona_recuperato) && $IDpersona_recuperato!="nessunvalore") {
        $conditions[]="IDprestazione IN (SELECT IDprestazione FROM prestazioni WHERE IDpersona=$IDpersona_recuperato)";
}
if (!empty($cerca_idprestazione)) {
  $conditions[]="IDprestazione='$cerca_idprestazione'";
}
if (!empty($dataemissione)) {
  $conditions[]="dataemissione <= '$dataemissione'";
}
if (!empty($valore)) {
  $conditions[]="valorelordo = '$valore'";
}
if (!empty($codicecontrollo)) {
  $conditions[]="codice = '$codicecontrollo'";
}
if (!empty($numero)) {
  $conditions[]="numero = '$numero'";
}
if ($consegnato=="si"){
  $conditions[]="consegnato=1";
}elseif ($consegnato=="no") {
  $conditions[]="consegnato=0";
}

if (count($conditions) > 0) {
  $query_voucher .=" WHERE " . implode(' AND ', $conditions);
}

if ($ordinaper=="numero") {
  $query_voucher .=" ORDER BY numero";
}elseif ($ordinaper=="idprestazione") {
  $query_voucher .=" ORDER BY IDprestazione";
}elseif ($ordinaper=="valore") {
  $query_voucher .=" ORDER BY valorelordo DESC";
}elseif ($ordinaper=="consegnato") {
  $query_voucher .=" ORDER BY consegnato";
}

Than the count and sum data will go in a table like 比计数和总和数据会像表格一样

 $row_voucher1=mysql_fetch_row($query_voucher);

echo "<form method='POST' name='elimina' id='elimina' action='php/elimina_voucher.php'>
    <table class='table table-striped' style='margin-top:70px; width: 60%;'>
    <caption>Riassunto Query Eseguita</caption>
    <tr>
        <th></th>
        <th>liberi</th>
        <th>prenotati</th>
        <th>consegnati</th>
        <th>Totale</th>
    </tr>";
echo "<td>
        <tr>" . $row_voucher1[quantitaliberi] . "</tr>
        <tr>" . $row_voucher1[quantitaprenotati] . "</tr>
        <tr>" . $row_voucher1[quantitapagati] . "</tr>
        <tr>" . $row_voucher1[valoreliberi] . "</tr>
      </td>";

    echo "</table>";

Than the * in the query is used to retrieve more general data in another table like this 比查询中的*用于检索另一个表中的更多常规数据

 while ($row_voucher=mysql_fetch_row($risultato_query_voucher)) {
 if ($row_voucher[1]!=0){
    $risultato_query_prestazioni=mysql_query("SELECT * FROM prestazioni WHERE IDprestazione='$row_voucher[1]'");
}

  echo "<tr>
          <th><div class='showdata'>+</div><div style='margin-top:-20px;margin-left:15px;'><input type='checkbox' name='IDvoucher' id='IDvoucher' value='" . $row_voucher[0] . "'></div></th>
          <td>" . $row_voucher[2] . "</td>
          <td>" . $row_voucher[3] . "</td>
          <td>" . date_format( new DateTime($row_voucher[4]), 'd/m/Y' ) . "</td>
          <td>" . $row_voucher[6] . "€</td>
          <td>"; if ($row_voucher[1]==0){echo "<font color=green>Libero</font>";}else{echo "$row_voucher[1]";}echo "</td>
          <td>";if ($row_voucher[7]==0) {
              echo "No";
            }else{
              echo "Si";
            } echo "</td>
          <td>";if ($row_voucher[7]==1){echo date_format( new DateTime($row_voucher[8]), 'd/m/Y' );} echo "</td>
        </tr>";

before using the sum and count and the query was a simply SELECT * FROM voucher everything in the second table was working fine. 在使用sumcount之前,查询是一个简单的SELECT * FROM voucher ,第二个表中的所有内容都正常工作。 Now i got a pretty common error Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /volume1/web/voucher/index6.php on line 285 sign of something wrong in the query (and not only there). 现在我得到了一个非常常见的错误Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /volume1/web/voucher/index6.php on line 285是查询中出错的标志(而不仅仅是那里)。

Any help is appreciated. 任何帮助表示赞赏。

Check $result before passing it to mysql_fetch_array. 在将结果传递给mysql_fetch_array之前检查$ result。 You'll find that it's false because the query failed. 您会发现它是错误的,因为查询失败了。 See the mysql_query documentation for possible return values and suggestions for how to deal with them. 有关可能的返回值以及如何处理它们的建议,请参阅mysql_query文档。

$result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'");

if($result === FALSE) { 
    die(mysql_error()); // TODO: better error handling
}

while($row = mysql_fetch_array($result))
{
    echo $row['FirstName'];
}

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

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