2014-02-12 63 views
0

多計數行數我有兩個表的評價和評分表rate_table返回在MySQL

ratingID,  ratingname,   userID.   
    1   RateA    admin  
    2   RateB    Admin 
    3   RateC    0001 

在rate_table我

ratingID  come      userID 
1   xxxx      0001 
2   xxxxx      0001 
2   xxxxx      0001 
3   xxxx      0001 
2   xxxxx      0001 
1   xxxxx      0001 

我期待一個看起來像這樣的結果。不工作。

RateA 2 
RateB. 3 
RateC 1 

這是我曾嘗試:

<?php public function rating() { 
$this -> jpt_connect(); 
$userID= $this ->users_info('userID'); 
$rating_ar = array(); 
$sql =mysql_query("SELECT 
         rt.ratingname, 
         COUNT(follows.userID) as id 
        FROM rating as rt 
        LEFT JOIN rate_table as tr 
        ON. (tr.userID = '$userID') 
         AND (tr.ratingID = rt.ratingID) 
        WHERE rt.userID ='admin' 
         OR rt.userID='$userID' ") 
or die(mysql_error()); 
while($row= mysql_fetch_array($sql)) { 
$rating_ar[] =$row; 
} 
return $rating_ar; 
} 
$j = new jkp(); 
$array =$j -> rating(); 
?> 

我怎樣才能算率排聯同等級名稱 請我如何可以訪問陣列變種; 我想發送所有來自數組中的數據。
我試過,但沒有發生提前感謝

+0

其中是'rate_table.followerID'定義? –

回答

0

通過評價對象加入收視率的評價對象和組:

select o.name, count(1) votes 
from votes v 
join objects o ON o.id = v.object_id 
group by o.id; 

由於看到http://sqlfiddle.com/#!7/c1ebb/1

我已經改名爲表,因爲我沒有「T得到你:

OBJECTS (id, name) 
VOTES (object_id) 

(只相關列)

編輯或者也許我根本不理解這個問題。如下?用戶是相關的?