gpt4 book ai didi

php - 返回mysql中的多个count行数

转载 作者:行者123 更新时间:2023-11-30 00:41:36 25 4
gpt4 key购买 nike

我有两个表 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 数组;我想发送来自该数组内数据库的所有数据。
我尝试过,但没有任何反应,提前致谢

最佳答案

将评级加入评级对象并按评级对象分组:

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 )

(仅相关列)

编辑或者也许我根本不理解这个问题。跟随?用户相关吗?

关于php - 返回mysql中的多个count行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21732624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com