mysql从多个表混合计数
我正在努力实现总的主题数,总的帖子数,最后一篇文章为给定的部分,请找到数据库结构和查询如下mysql从多个表混合计数,mysql,Mysql,我正在努力实现总的主题数,总的帖子数,最后一篇文章为给定的部分,请找到数据库结构和查询如下 fcats | id | title | section | +----+--------+---------+ | 1 | test | gd | +----+--------+---------+ ftopics | id | title | cat_id | +----+--------+---------+ | 1 | test1 | 1 | +----
fcats
| id | title | section |
+----+--------+---------+
| 1 | test | gd |
+----+--------+---------+
ftopics
| id | title | cat_id |
+----+--------+---------+
| 1 | test1 | 1 |
+----+--------+---------+
fposts
| id | post | topic_id | posted_by
+----+-------+----------+---------+
| 1 | post | 1 | user |
+----+-------+----------+---------+
当前查询
通过使用上面的查询,我只能得到给定部分的总主题,但我对如何获得给定部分的总文章数和最后一篇文章感到困惑。请帮助,谢谢。回答的第一部分应使用count distinct,第二部分使用子查询:
SELECT c.id,
c.title ,
count( distinct t.cat_id) AS total_topics ,
count( distinct p.id) AS total_posts ,
(select p2.id
from ne_forum_posts p2
inner join ne_forum_topics t2 on p2.topic_id = t2.id
inner join ne_forum_sub_cats c2 on c2.id = t2.cat_id
where c2.id = c.id
order by p2.id desc
limit 1) as last_post_id
FROM ne_forum_sub_cats c LEFT OUTER JOIN
ne_forum_topics t on c.id = t.cat_id LEFT OUTER JOIN
ne_forum_posts p on p.topic_id = t.id
WHERE section = "gd"
所有打字错误都已修复并测试。对于答案的第一部分,您应该使用count distinct,对于第二部分,您应该使用子查询:
SELECT c.id,
c.title ,
count( distinct t.cat_id) AS total_topics ,
count( distinct p.id) AS total_posts ,
(select p2.id
from ne_forum_posts p2
inner join ne_forum_topics t2 on p2.topic_id = t2.id
inner join ne_forum_sub_cats c2 on c2.id = t2.cat_id
where c2.id = c.id
order by p2.id desc
limit 1) as last_post_id
FROM ne_forum_sub_cats c LEFT OUTER JOIN
ne_forum_topics t on c.id = t.cat_id LEFT OUTER JOIN
ne_forum_posts p on p.topic_id = t.id
WHERE section = "gd"
所有打字错误都已修复并测试。可能是这样的:
SELECT
id,
title ,
(
SELECT
count(ftopics.id)
FROM
ftopics
WHERE
ftopics.cat_id = fcats.id
) AS total_topics,
(
SELECT
COUNT(distinct fposts.id)
FROM
ftopics
JOIN fposts
ON ftopics.id=fposts.topic_id
WHERE
ftopics.cat_id = fcats.id
),
(
select
fposts.id
from fposts
inner join ftopics on fposts.topic_id = ftopics.id
inner join fcats c2 on c2.id = ftopics.cat_id
where fcats.id = c2.id
order by fposts.id desc
limit 1
) as last_post_id
FROM fcats
WHERE section = "gd"
也许是这样的:
SELECT
id,
title ,
(
SELECT
count(ftopics.id)
FROM
ftopics
WHERE
ftopics.cat_id = fcats.id
) AS total_topics,
(
SELECT
COUNT(distinct fposts.id)
FROM
ftopics
JOIN fposts
ON ftopics.id=fposts.topic_id
WHERE
ftopics.cat_id = fcats.id
),
(
select
fposts.id
from fposts
inner join ftopics on fposts.topic_id = ftopics.id
inner join fcats c2 on c2.id = ftopics.cat_id
where fcats.id = c2.id
order by fposts.id desc
limit 1
) as last_post_id
FROM fcats
WHERE section = "gd"
如果该节没有任何post,也包括null大小写
如果该部分没有任何帖子,也包括空案例谢谢帮助,字段列表中的抛出错误列“id”在回答中是模糊的。也许他们是更多的错误,这是没有测试。你们也应该贡献……感谢你们的帮助,它在字段列表中的抛出错误列“id”在答案中是模糊的。也许他们是更多的错误,这是没有测试。你也应该参与……你能更新查询吗?它甚至可以从posts表中获取post和id。如果我从ftopics中添加此SELECT title,其中id=SELECT topic\U id from fposts,其中id=MAXC.id作为最后一个\U post\U topic\U title获取主题的标题吗?你能更新查询吗?它甚至可以从post获取post和id吗table.如果我从ftopics中添加此选择标题(其中id=从fposts中选择主题\u id,其中id=MAXC.id)作为最后一个\u post\u topic\u标题以获取主题\u id的标题,这是否好?为什么否决?诚然,这不是一个复杂的SQL问题,但对我来说似乎是有效的。为什么会有反对票?诚然,这不是一个复杂的SQL问题,但对我来说似乎是有效的。