简体   繁体   English

SQL,如何不计算0值

[英]SQL , how not to count 0 values

am trying to find a total vote using this code, I have 12 criteria; 我正在尝试使用此代码进行总投票,我有12条条件; So am adding up the TOTAL for each SHOP_ID and then dividing by 12( the amount of columns used to make total). 因此,将每个SHOP_ID的TOTAL加起来,然后除以12(用于总计的列数)。 COUNT(shop_id) finds the number of shops, So for example if I have 2 shop_id inputs one with a total of 0 ( 12 entries of 0) and the other a total of 60 which is divided by the 12 criteria's then divided by shop_id count of 2 (60/12)/2 = 2.5 I would like the result to be 5. Some how not counting the column's with 0 in the division. 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';
}

Would it be better if i supply an image so visually display what am trying to achieve? 如果我提供图像以视觉方式显示要实现的效果会更好吗?

each row is NOT 1 vote, each shop_id is 12 votes. 每行不是1票,每个shop_id是12票。 Each column category name: comfort + service + ambience, is a column id and an entry into that field is 1 vote, it counts the 12 as 1 vote, when if you vote for 1 criteria the other 11 are 0, and returns the wrong results, as division by 2 will half the correct answer 每个列类别名称:舒适性+服务+环境,是列ID,并且该字段中的条目为1票,它将12计为1票,如果您对1个条件投票,则其他11则为0,并返回错误结果,除以2将得到正确答案的一半

I have uploaded a visual example and shortened the rating columns from 12 to 7 for testing purposes. 我已经上传了一个直观的示例,出于测试目的,将评分列从12缩短为7。 表示例

try to use HAVING clause: 尝试使用HAVING子句:

select
...
from
...
where
...
having COUNT(shop_id) > 0
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
HAVING total > 0

I'm pretty sure your query is not quite right, but that could be a typo. 我很确定您的查询不太正确,但这可能是一个错字。

Here might be a better query: 这可能是一个更好的查询:

SELECT shop_id, SUM(comfort + service + ambience + mood + spacious + 
experience + cleanliness+ price + drinks + food + music + toilets)/12 as total_rating                 
FROM ratings         
GROUP BY shop_id
HAVING total > 0

Personally, I think if someone is getting 0's across the board, that's worth noting, but the above should give you the average rating per shop and filter out any that are not at least 1. 就个人而言,我认为如果有人的总得分为0,那是值得注意的,但是以上所述应该可以为您提供每家商店的平均评分,并过滤掉不低于1的所有评分。

If each category is from a scale of 0 - 5 and there are 12 categories, that means that their percent score is the (sum of the individual scores)/(possible total) * 100, or, in this case, the sum of the individual scores/.6 (works out the same way). 如果每个类别的评分范围是0-5,并且有12个类别,则表示它们的百分比得分是(各个得分的总和)/(可能的总分)* 100,或者在这种情况下,是单个分数/.6(计算方法相同)。 If you want a "star rating" where the number of stars is equal to the top score of each category, that's just the average. 如果您想要一个“星级评定”,其中星级数量等于每个类别的最高得分,那只是平均值。 In that case, I'd round off to one decimal. 在这种情况下,我将四舍五入为小数点后一位。 So if you want to go that route, here is the query I would use: 因此,如果您想走那条路线,这是我要使用的查询:

   SELECT shop_id, 
          Ceiling(
                 Sum(comfort + service + ambience + mood + spacious + 
                      experience + cleanliness + price + 
                      drinks + food + music + toilets
           ) /. 6) 
          AS  percent_rating, 
          ROUND(
            SUM(
              comfort + service + ambience + mood + 
              spacious + experience + cleanliness + 
              price + drinks + food + music + toilets
            )/12, 1)
          as star_rating
   FROM   ratings 
   GROUP  BY shop_id 

Now, if you want to have it laid out where each score is out of 100%, then it would be (cat_score/5) * 100 or (cat_score/.05). 现在,如果您希望将每个分数都超出100%的位置进行布局,则应为(cat_score / 5)* 100或(cat_score / .05)。 I'm not sure if there is an easy way to apply to that all of the categories without having to explicitly do the math for each field, but really I don't think you need to go to the trouble to figure out that 4/5 is 80%. 我不确定是否有一种简便的方法可以应用于所有类别,而不必显式地对每个字段进行数学运算,但实际上我认为您不必费心找出4 / 5是80%。

As far as what to do regarding a default 0 versus an intentional 0, you have no way of knowing the difference. 至于针对默认0与有意0的处理方式,您无法知道其区别。 If someone didn't feel moved enough to change it from a 0 (or a blank), it must not have been that terrific. 如果某人感觉不到移动到足以将其从0(或空白)更改的程度,那么它一定不是那么了不起。 Best bet is to set the default to the middle (3) and most folks will mark it lower if it was actually so bad it was a 0. Similarly, if you are giving the option of leaving it blank (which isn't 0, it's NULL), then you can either move it to 3 (which works out the same. If they left it all blank, that's the same as saying "so average I didn't fill it out) or you can make it some number you think is fair (1 or 2, maybe). Throwing it out is a pain mysql, and in statistics leads to misleading data (think hanging chad combined with language barrier so they left it blank). In the end, moving blank to the middle will be fine because you should have enough data when it's all said and done to get a standard deviation and other more valuable stats, rather than rely on plain averages (where a handful of bad reviews can throw off the whole curve). 最好的选择是将默认值设置为中间值(3),如果实际情况很糟糕,默认值为0,大多数人会将此值标记为较低。类似地,如果您将其保留为空白(不为0,它为NULL),那么您可以将其移至3(效果相同。如果他们都将其留为空白,这与说“所以平均而言我没有填写”)相同,也可以将其设为一个数字认为是公平的(可能是1或2),将其扔掉是一个痛苦的mysql,并且在统计信息中会导致误导性数据(认为将chad与语言障碍相结合,因此将它们留为空白)。最后,将空白移到中间会很好的,因为说了算,您应该有足够的数据来获取标准偏差和其他更有价值的统计信息,而不是依赖于简单的平均数(在这里,一些糟糕的评论可能会拖垮整个曲线)。

添加到查询的末尾:

HAVING comfort + service + ambience + mood + spacious + experience + cleanliness+ price + drinks + food + music + toilets != 0

Your query is not quite right. 您的查询不太正确。 It's missing a parenthesis. 它缺少括号。

Correct one: 正确的一个:

$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 having COUNT(shop_id) > 0");

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

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