从Access 2007中的多个表中使用COUNT进行SELECT

时间:2011-07-29 22:14:23

标签: sql select count ms-access-2007

我有2个查询称为“3个月查询”和“6个月查询”。现在我正在创建一个包含查询作为记录源的报告。我想计算3个月查询中的行数并将其标记为Need3Month,以及6个月查询中的行数并将其标记为Need6Month。我尝试了以下无法正常工作:

SELECT COUNT([3 Month Query].[ID]) AS Need3Month, COUNT([6 Month Query].[ID]) AS Need6Month FROM [3 Month Query], [6 Month Query];

我也尝试过:

SELECT COUNT([3 Month Query].[ID] AS Need3Month FROM [3 Month Query]
UNION
SELECT COUNT([6 Month Query].[ID] AS Need6Month FROM [6 Month Query];

这会返回正确的数字,但会在Need3Month列下列出它们。

我该怎么办?非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

SELECT
  (SELECT COUNT([ID]) FROM [3 Month Query]) AS Need3Month,
  (SELECT COUNT([ID]) FROM [6 Month Query]) AS Need6Month

更新以回复评论:

SELECT
  COUNT([ID]) AS Need3Month,
  (SELECT COUNT([ID]) FROM [6 Month Query]) AS Need6Month
FROM [3 Month Query]

更新2

这也应该有效(我认为):

SELECT
  Cnt6.Cnt AS Need3Month,
  Cnt3.Cnt AS Need3Month
FROM
  (SELECT COUNT([ID]) AS Cnt FROM [3 Month Query]) AS Cnt3,
  (SELECT COUNT([ID]) AS Cnt FROM [6 Month Query]) AS Cnt6

答案 1 :(得分:3)

不确定它是否在MS Access中有效但在SQL中你可以这样做:

SELECT 
    (SELECT COUNT(ID) FROM [3 Month Query]) Need3Month, 
    (SELECT COUNT(ID) FROM [6 Month Query]) Need6Month