我正在尝试使用此代码查找总票数,我有 12 个标准;因此,我将每个 SHOP_ID 的 TOTAL 相加,然后除以 12(用于总计的列数)。COUNT(shop_id) 查找商店的数量,例如,如果我有 2 个 shop_id 输入一个,总共 0(12 个 0 条目),另一个总共 60,除以 12 个标准,然后除以 shop_id 计数2 (60/12)/2 = 2.5 我希望结果为 5。有些如何不计算除法中 0 的列。
$sel = mysql_query("SELECT SUM(comfort + service + ambience + mood + spacious + 
    experience + cleanliness+ price + drinks + food + music + toilets)/(12/COUNT(shop_id) as total                  
    FROM ratings         
    WHERE shop_id = $shop_id");                 
if(mysql_num_rows($sel) > 0){
    while($data = mysql_fetch_assoc($sel)){
        $total = $total + $data['total']; 
        $rows++;
    }
    $perc = ($total/$rows);
    return round($perc,2);
} else {
    return '0';
}
如果我提供图像以便直观地显示要实现的目标会更好吗?
每行不是 1 票,每个 shop_id 是 12 票。每列类别名称:comfort + service + ambience,是一个列 id,该字段的条目为 1 票,它将 12 计为 1 票,如果您为 1 个标准投票,则其他 11 个为 0,并返回错误结果,因为除以 2 将是正确答案的一半
出于测试目的,我上传了一个可视化示例并将评分列从 12 缩短到 7。
