0

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

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;

我怎样才能使这个计数返回的记录数?

4

2 回答 2

3

一般来说,

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

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

于 2013-02-15T00:59:10.527 回答
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
于 2013-02-14T23:56:22.103 回答