PHP를 통해 조건부 SQL 쿼리를 생성해야합니다.이 필드에서는 일부 필드의 합계와 개수가 계산됩니다. 그것을 자르고 내가 작성한 질의로 바로 가자. 개수 및 합계 데이터보다 SQL - 여러 필드를 선택하십시오.
$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";
}
이
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>";
같은 다른 테이블에서보다 일반적인 데이터를 검색하는 데 사용되는 쿼리의
*
보다
$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>";
같은 테이블에 갈 것입니다 sum
및 count
을 사용하기 전에
을 사용하고 쿼리는 단순히 SELECT * FROM voucher
이었고 두 번째 테이블의 모든 항목이 정상적으로 작동했습니다. 이제 꽤 일반적인 오류가 있습니다. Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /volume1/web/voucher/index6.php on line 285
뭔가 잘못된 쿼리가 있습니다 (뿐만 아니라).
도움을 주시면 감사하겠습니다.
로그 전체 쿼리는 mysql dp에서 실행하고 오류보고 – siddhesh
@siddhesh 미안합니다. 문제가 쿼리에 있다는 것을 알고 있기 때문에 확인할 수 있습니까? – TheMerovingian
@siddhesh ehm ... 제발? – TheMerovingian