2012-08-23 107 views
1

我正在爲車展寫腳本。用戶選擇汽車號碼並根據各種標準投票1-5。有多個用戶爲同一輛車投票,但他們只能爲每輛車投票一次,我檢查了這一點。PHP/SQL投票並計算結果

現在我有一個獨特的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查找總分?

回答

2

只需使用SUM和ORDER BY:

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

這應該做的 「絕招」

+0

簡單,謝謝! –

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; 
2
SELECT carNum, sum(exterior + interior + engine) as Score 
FROM Judge_Votes 
WHERE gatagory = '$catNum' 
GROUP BY carNum 
ORDER BY carNum