2010-12-22 50 views
0
<? 
$tablae = mysql_query("SELECT * FROM order_history where (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "' GROUP BY user_id"); 
while ($order = mysql_fetch_array($tablae)) { 

?> 
<tr> 
<? 
$tablaes = mysql_query("SELECT * FROM members where id='$order[user_id]'"); 
$user = mysql_fetch_array($tablaes); 
$idsd=$user['id']; 
$rPaid=mysql_query("SELECT SUM(`price`) AS total FROM order_history WHERE (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'"); 
$hdPaid = mysql_fetch_array($rPaid); 
$sPaid=mysql_query("SELECT SUM(`price`) AS total FROM order_history WHERE user_id='$idsd' AND (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'"); 
while ($hPaid = mysql_fetch_array($sPaid)) { 
?> 
<td><?=$user['username']?></td> 
<td><?=$hPaid['total']?></td> 
<? 
} 
?> 

</tr> 

<? } ?> 

這讓我這個結果http://dl.dropbox.com/u/14384295/test.jpeg訂購總計Sum函數使用

我要訂購按降序價格總額。

我需要

$sPaid=mysql_query("SELECT SUM(`price`) AS total FROM order_history WHERE user_id='$idsd' AND (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'"); 

上總被降序排序。

+0

將非日期傳遞給您的腳本會導致查詢失敗。如果你有錯誤,我可能會得到你的表的名字和更多的信息,我可以用它來直接攻擊。 – DampeS8N 2010-12-22 15:26:49

+0

日期在腳本中指定...這不是我已發佈的整個頁面。 – friendishan 2010-12-22 15:28:07

回答

0

你可以使用客戶端與JavaScript的排序,有一些很好的jQuery插件可以做到這一點。

或者您必須全部重寫您的代碼才能使用連接和分組來完成單個sql。

但我不能真正遵循$ rPaid,$ hPaid和$ sPaid的邏輯,所以我無法幫到你。

+0

$ rPaid用於定義總計..不包括在發佈的代碼中.. 您可以發佈一些鏈接到任何jQuery插件嗎? – friendishan 2010-12-22 15:44:42

1

在你的SQL查詢中使用GROUP BY指令時要十分小心。所有在結果中並且不是聚集表達式的列(表達式應該是count,SUM,max等,在組上而不在行上)應該在你的group by表達式中; 在這裏您使用select *,您應該嘗試列出實際列,並在您的組中獲取此列表,或者僅使用SELECT user_id。

大多數數據庫都會阻止你通過查詢來運行這樣不是很好的組,但是MySQl不會讓你陷入困境,並不意味着如果你不重新考慮這個規則,他不會給你完全錯誤的結果(所有非聚合的列必須在組中)。

然後,您應該能夠通過重新使用此表達式而不是他的別名在順序子句中通過聚合表達式進行排序。