2017-09-16 184 views
0

我試圖通過2個計算已經在聲明中下令我的select語句的結果如果是有道理的劃分兩個數字..正確的語法在PHP Select語句

$sql = "SELECT Make, Model,COUNT(*) as total ,SUM(FrontGross) as sumGross FROM table GROUP BY Model ORDER BY (sumGross/total) DESC LIMIT 20"; 
$result = $conn->query($sql); 
if ($result->num_rows > 0) { 
    while($row = $result->fetch_assoc()) { 

     } 
    } 

它不工作與ORDER BY(sumGross/total)。用總和除以sumGross的正確語法是什麼?

+0

顯示您的樣本數據和預期結果集 –

+0

*您好*正確嗎?我不清楚問題在哪裏。你使用ORDER BY子句命令查詢結果,這種方式不起作用? – David

+0

我只是希望在select語句中獲得正確的語法,而不是太擔心輸出。 –

回答

0
SELECT 
    Make, 
    Model, 
    COUNT(*) as total, 
    SUM(FrontGross) as sumGross, 
    SUM(FrontGross)/COUNT(*) as win 
FROM table 
GROUP BY Model 
ORDER BY win ASC 
LIMIT 20";