简体   繁体   English

MS Access SQL - 记录集中每条记录的计数

[英]MS Access SQL - Count for each record in a recordset

Good day all, hope you can help!大家好,希望能帮到你!

I have been toiling with this Query for a while now, and I am sure it will be relatively simple to fix!我一直在处理这个查询一段时间,我相信修复它会相对简单!

I use the query to return all of the values from one table, along with a count of votes from one table, and a count of comments from another.我使用查询返回一个表中的所有值,以及一个表中的投票数和另一个表中的评论数。

I have got it to work for loading an individual record我可以用它来加载单个记录

DCount('[query_id]','[comments]','[query_id]=" & Target & "')

However if I add something similar to the query that returns every query_id, the count shows the same for them all.但是,如果我添加类似于返回每个 query_id 的查询的内容,则计数对它们显示的都是相同的。

Is there a different function I can use than DCount to achieve this?我可以使用与 DCount 不同的功能来实现这一点吗?

My previous issue was using count, and as the query had non unique data it was counting all votes from a person (ie if I had made 6 votes, the count would show as 6 for any record my user id was attached to)我之前的问题是使用计数,并且由于查询具有非唯一数据,它正在计算一个人的所有选票(即,如果我投了 6 票,对于我的用户 ID 附加到的任何记录,计数将显示为 6)

Happy to provide any further detail regarding the query.很高兴提供有关查询的任何进一步详细信息。

You may be after something like:你可能在追求类似的东西:

DCount("*","[comments]","[person_id] = " & [Target] & "")

where [Target] is the field holding the PersonId of the other table.其中 [Target] 是保存另一个表的 PersonId 的字段。

I perhaps didn't ask the question in the best possible way, but I have managed to get the results I expected by using sub select statements in the SELECT clause我可能没有以最好的方式问这个问题,但我已经设法通过在 SELECT 子句中使用子选择语句来获得我预期的结果

Just in case someone else doesn't know how to word or ask a question, Andre posted a very helpful comment to my original question above, advising the following link.以防万一其他人不知道如何表达或提问,安德烈对我上面的原始问题发表了非常有帮助的评论,并提供了以下链接。

How to ask a good SQL question如何提出一个好的 SQL 问题

SELECT issues.query_id, issues.query_raised_by, issues.query_raised_date, issues.query_summary, issues.query_status, issues.query_status_date, issues.query_detail, issues.query_response
(SELECT COUNT(*) FROM vote WHERE query_id = issues.query_id) AS voteCount, 
(SELECT COUNT(*) FROM comments WHERE query_id = issues.query_id) AS commentCount

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM