簡體   English   中英

在mysql中返回多個計數行數

[英]Return multiple count rows number in mysql

我有兩個表rating和rate_table

在評分表上我有

 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();
?>

我如何計算與評級名稱結合在一起的利率行,請問如何訪問var數組; 我想發送所有來自該數組內部db的數據。
我嘗試了但沒有任何反應

將評級加入評級對象並按評級對象分組:

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所示

我已將表重命名,因為我沒有得到您的表:

OBJECTS ( id, name )
VOTES ( object_id )

(僅相關列)

編輯也許我根本不了解問題。 跟隨? 用戶相關嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM