1

我有一个从数据库打印记录的查询,但我不明白如何省略某些字段包含零的记录。可能包含零的字段是townhalls, org_pressconf, rep_doors, vol_doors, contacts, phones, coffeehours, newsarticles, 和mediahits。如果我上面提到的每个字段都等于零,我该如何编写查询以省略这些结果?

// START Organizer Report   
// sending query
$result = mysql_query("SELECT  
Concat(last_name, ' , ', first_name) as Representative, 
sum(townhalls) as Townhalls, 
sum(org_pressconf) as PressConference, 
sum(rep_doors) as RepDoors, 
sum(vol_doors) as VolunteerDoors, 
sum(contacts) as Contacts, 
sum(phones) as Phones, 
sum(coffeehours) as CoffeeHours, 
sum(newsarticles) as NewsArticles, 
sum(mediahits) as MediaHits 
FROM reports 
join representatives on reports.rep = representatives.id 
join users on reports.username = users.username 
WHERE date BETWEEN '$from' AND '$to' AND role = 'organizer'
Group By representative Order By representative;");
if (!$result) {
    die("Query to show fields from table failed");
}


$fields_num = mysql_num_fields($result);

echo "<h2>Organizer Report <small>($from to $to)</small></h2>";
echo "<table border='1' cellspacing='0' cellpadding='3' cellspacing='0' cellpadding='3'>
<tr>";

// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";

// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}

mysql_free_result($result);

    echo "</table>\n";
// END Organizer Report
4

1 回答 1

0

如果要省略所有这些字段都为零的行,请添加以下 HAVING 子句。

HAVING (townhalls + org_pressconf + rep_doors + vol_doors + contacts + phones + coffeehours + newsarticles + mediahits) > 0
于 2013-08-16T15:00:17.120 回答