PHP / SQL投票和计算结果

时间:2012-08-23 19:48:58

标签: php mysql sql vote

我正在为车展写剧本。用户选择车号并按各种标准投票1-5。有多个用户投票给同一辆车,但他们只能为每辆车投票一次,我检查了这一点。

现在我有一张独特的carNumber表,每辆车每个标准有1-5票。

以下是我如何找到每个用户投票的总分

SELECT carNum, exterior+interior+engine AS Score FROM Judge_Votes WHERE catagory = '$catNum' ORDER BY carNum

Reults 
CarNum: Score:
18  11
14  8
13  15
12  8
12  11
2   14

我想将每个用户的总分加到最终得分结果中。 IE车12总得分为19。

我的问题。如何使用sql或php找到总分?

3 个答案:

答案 0 :(得分:2)

只需使用SUM和ORDER BY:

SELECT carNum, SUM(exterior+interior+engine) AS Score 
    FROM Judge_Votes 
    WHERE catagory = '$catNum' 
    GROUP BY carNum

应该做“技巧”

答案 1 :(得分:2)

SELECT carNum, sum(exterior + interior + engine) as Score 
FROM Judge_Votes 
WHERE gatagory = '$catNum' 
GROUP BY carNum 
ORDER BY carNum

答案 2 :(得分:0)

以下应该是我认为的伎俩。逻辑是您按车辆分组结果(GROUP BY carNum),然后使用SQL的SUM函数构建。

SELECT SUM(exterior+interior+engine) as totalScore, carNum 
FROM Judge_Votes 
WHERE catagory = '$catNum' 
GROUP BY carNum
ORDER BY carNum;