我正在开发类似论坛的网站 我想将下一个信息放入div
Category: Cars
Topics: 27 Comments:5436
Last Comment: 01.04.2020 //php formated
我在这里计算帖子数
SELECT *
FROM posts as p
WHERE p.category= :category
$sql->execute();
我在这里计算评论数
SELECT *
FROM comments as c
LEFT JOIN posts as p ON c.id_post = p.id_post
WHERE p.category= :category
$sql->execute();
在这里我找到了最后的评论日期
SELECT max(c.date) as maxdate
FROM comments as c
LEFT JOIN posts as p ON c.id_post = p.id_post
WHERE p.category= :category
GROUP BY c.id_comment
$sql->execute();
我最大的问题是如何将所有这三个sql语句和3个pdo执行合并到一个sql语句中(单个pdo执行)?
答案 0 :(得分:1)
您可以加入并聚集。考虑:
select
p.category,
count(distinct p.id_posts) no_posts,
count(c.id_post) no_comments,
max(c.date) date_last_comment
from posts p
left join comments c on c.id_post = p.id_post
where p.category= :category
group by p.category