另一个表上的访问计数不起作用

发布于 2024-09-11 04:20:10 字数 637 浏览 9 评论 0原文

我有一个博客系统的 Posts 表和 PostComments 表。我想按评论计数对帖子进行计数和排序,但我的查询不起作用。:

SELECT Posts.PostID, Posts.DateCreated, Posts.Title, Posts.Description,
Posts.Hits, (SELECT Count(CommentID) FROM PostComments WHERE 
PostComments.PostID=Posts.PostID AND PostComments.IsApproved=True) AS
CommentCount FROM Posts ORDER BY Posts.PostID DESC;

我也尝试过:

SELECT Posts.PostID, Posts.DateCreated, Posts.Title, Posts.Description,
Posts.Hits, Count([CommentID]) AS CommentCount FROM Posts INNER JOIN PostComments
ON Posts.PostID = PostComments.PostID;

但出现错误“您试图执行不包含指定表达式“PostID”的查询作为聚合函数的一部分。”

I have a Posts table and PostComments table of a blog system. I want to count and sort the posts by comment count but my query won't work.:

SELECT Posts.PostID, Posts.DateCreated, Posts.Title, Posts.Description,
Posts.Hits, (SELECT Count(CommentID) FROM PostComments WHERE 
PostComments.PostID=Posts.PostID AND PostComments.IsApproved=True) AS
CommentCount FROM Posts ORDER BY Posts.PostID DESC;

I also tried:

SELECT Posts.PostID, Posts.DateCreated, Posts.Title, Posts.Description,
Posts.Hits, Count([CommentID]) AS CommentCount FROM Posts INNER JOIN PostComments
ON Posts.PostID = PostComments.PostID;

But have error "You tried to execute a query that does not include specified expression 'PostID' as a part of an aggregate function."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方圜几里 2024-09-18 04:20:14
SELECT 
    Posts.PostID, 
    Posts.DateCreated, 
    Posts.Title, 
    Posts.Description, 
    Posts.Hits, 
    dr.CommentCount 
FROM Posts p
INNER JOIN 
    (SELECT PostID, Count(CommentID) as CommentCount FROM PostComments WHERE  
        PostComments.IsApproved=True GROUP BY PostId) dr ON dr.PostID = p.PostID
ORDER BY dr.CommentCount DESC;
SELECT 
    Posts.PostID, 
    Posts.DateCreated, 
    Posts.Title, 
    Posts.Description, 
    Posts.Hits, 
    dr.CommentCount 
FROM Posts p
INNER JOIN 
    (SELECT PostID, Count(CommentID) as CommentCount FROM PostComments WHERE  
        PostComments.IsApproved=True GROUP BY PostId) dr ON dr.PostID = p.PostID
ORDER BY dr.CommentCount DESC;
脸赞 2024-09-18 04:20:13

我是一个十足的 Access 菜鸟,但请尝试按非聚合列进行分组的第二个。

    SELECT 
       Posts.PostID
       ,Posts.DateCreated
       ,Posts.Title
       ,Posts.Description
       ,Posts.Hits
       ,Count([CommentID]) AS CommentCount 
    FROM Posts 
       INNER JOIN PostComments ON Posts.PostID = PostComments.PostID
    GROUP BY
       Posts.PostID
       ,Posts.DateCreated
       ,Posts.Title
       ,Posts.Description
       ,Posts.Hits
   ORDER BY
      Count([CommentID]);

也许您必须在 MS-Access 中将 JOIN 行放入大括号中。

I'm a complete Access noob, but try the second one with a grouping by the non-aggregated columns.

    SELECT 
       Posts.PostID
       ,Posts.DateCreated
       ,Posts.Title
       ,Posts.Description
       ,Posts.Hits
       ,Count([CommentID]) AS CommentCount 
    FROM Posts 
       INNER JOIN PostComments ON Posts.PostID = PostComments.PostID
    GROUP BY
       Posts.PostID
       ,Posts.DateCreated
       ,Posts.Title
       ,Posts.Description
       ,Posts.Hits
   ORDER BY
      Count([CommentID]);

Maybe you have to put the JOIN row into braces in MS-Access.

枕梦 2024-09-18 04:20:13

尝试

SELECT Posts.PostID, Posts.DateCreated, Posts.Title, Posts.Description,
Posts.Hits, Count([CommentID]) AS CommentCount FROM Posts INNER JOIN PostComments
ON Posts.PostID = PostComments.PostID
GROUP BY Posts.PostID, Posts.DateCreated, Posts.Title, Posts.Description, Posts.Hits
ORDER BY Count([CommentID]) DESC

Try

SELECT Posts.PostID, Posts.DateCreated, Posts.Title, Posts.Description,
Posts.Hits, Count([CommentID]) AS CommentCount FROM Posts INNER JOIN PostComments
ON Posts.PostID = PostComments.PostID
GROUP BY Posts.PostID, Posts.DateCreated, Posts.Title, Posts.Description, Posts.Hits
ORDER BY Count([CommentID]) DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文