可怕的访问数据库问题

时间:2013-02-14 23:53:29

标签: sql ms-access

我有一个来自客户端的可怕数据库,我需要计算查询结果的数量,如下所示:

SELECT
  Offices.OfficeID
, ContractsBooksCommodities.CommodityID
  FROM ((((Offices
INNER JOIN tbl_Sales
        ON Offices.CompanyID = tbl_Sales.CompanyID)
INNER JOIN ContractBooks
        ON tbl_Sales.CompanyID = ContractBooks.CompanyID)
INNER JOIN ContractsBooksAds
        ON ContractBooks.ContractNum = ContractsBooksAds.ContractNum)
INNER JOIN ContractsBooksBrands
        ON ContractsBooksAds.ContractNum = ContractsBooksBrands.ContractNum)
INNER JOIN ContractsBooksCommodities
        ON ContractsBooksBrands.ContractNum = ContractsBooksCommodities.ContractNum;

如何计算返回的记录数?

2 个答案:

答案 0 :(得分:3)

一般来说,

select count(*)
from (
  your-select-query
)

将为您提供查询返回的记录数。

答案 1 :(得分:0)

COUNT和GROUP BY是我的猜测:

SELECT Offices.OfficeID, ContractsBooksCommodities.CommodityID, COUNT(*) AS COUNT
FROM ((((Offices INNER JOIN tbl_Sales ON Offices.CompanyID = tbl_Sales.CompanyID) INNER JOIN ContractBooks ON tbl_Sales.CompanyID = ContractBooks.CompanyID) INNER JOIN ContractsBooksAds ON ContractBooks.ContractNum = ContractsBooksAds.ContractNum) INNER JOIN ContractsBooksBrands ON ContractsBooksAds.ContractNum = ContractsBooksBrands.ContractNum) INNER JOIN ContractsBooksCommodities ON ContractsBooksBrands.ContractNum = ContractsBooksCommodities.ContractNum
GROUP BY Offices.OfficeID, ContractsBooksCommodities.CommodityID
ORDER BY Offices.OfficeID, ContractsBooksCommodities.CommodityID