我需要从3个表中检索数据:t_users
,t_forum
,t_replies
。
帖子,海报和该帖子的回复数量。
我已经让它像这样工作,但我看不到一个选项,可以通过大多数回复对它进行排序。
$_result= $_PDO->query("
SELECT * FROM t_users, t_forum
WHERE t_forum.d_op= t_users.d_user_id
$_orderBy");
if ($_result->rowCount() > 0) {
while($_row = $_result-> fetch(PDO::FETCH_ASSOC)){
$_postid = $_row['d_index'];
//count amount of replies per post
$_replycount = $_PDO ->query("SELECT COUNT(*)
FROM t_replies
WHERE d_reply_id = '$_postid'");
$_count = $_replycount ->fetchColumn();
}
}
//I have tried queries like this :
SELECT t_users.*, t_forum.*, COUNT(d_reply_id)
FROM t_users,t_forum, t_replies
WHERE t_forum.d_op= t_users.d_user_id
AND t_forum.d_index = t_replies.d_reply_id
//but this only retrieves one row and literally counts the d_reply_id value as the
value is numeric, thank you for reading.
答案 0 :(得分:1)
您的查询应如下所示:
SELECT /*put here field of thread and poster*/, COUNT(d_reply_id)
FROM t_users
join t_forum
on t_forum.d_op= t_users.d_user_id
left join t_replies
on t_forum.d_index = t_replies.d_reply_id
group by /*put here field of thread and poster*/
order by COUNT(d_reply_id) desc