2013-02-14 164 views
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; 

我怎樣才能使這個數的記錄數回?

回答

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 
+1

如果您無法獲得COUNT查詢,無論出於何種原因,只要運行查詢,然後在Recordset對象上使用RecordCount屬性(請注意,您可能必須先調用MoveLast)。 http://msdn.microsoft.com/en-us/library/office/bb208624%28v=office.12%29.aspx – 2013-02-15 00:00:09

3

一般來說,

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

會給你您的查詢返回的記錄數。