2016-01-31 65 views
1

我有以下的MS Access查詢,成功地檢索數據:女士訪問獲得行號從子查詢不表

SELECT stockInventory.purchaseId, stockInventory.itemId, item.itemName, stockInventory.unitId, unit.unitDesc, stockInventory.quantity, stockInventory.costPrice 
FROM unit INNER JOIN (item INNER JOIN stockInventory ON item.itemId = stockInventory.itemId) ON unit.unitId = stockInventory.unitId 
WHERE (((stockInventory.purchaseId)=1)) 

現在我想找回這些數據與行號!

我試過如下:

SELECT A.*, (SELECT COUNT(*) FROM A WHERE A.itemId>=itemId) as rowNo 
FROM 
(
SELECT stockInventory.purchaseId, stockInventory.itemId, item.itemName, stockInventory.unitId, unit.unitDesc, stockInventory.quantity, stockInventory.costPrice 
FROM unit INNER JOIN (item INNER JOIN stockInventory ON item.itemId = stockInventory.itemId) ON unit.unitId = stockInventory.unitId 
WHERE (((stockInventory.purchaseId)=1)) 
) AS A; 

但它說:Microsoft Access數據庫引擎無法找到輸入表或查詢 'A',如下圖:

enter image description here

如何我可以解決這個問題嗎?

+0

你能提供的查詢不計數呢?然後,更容易理解你想要達到的目標。 –

+0

這個查詢得到總記錄:SELECT COUNT(*)FROM SELECT stockInventory.purchaseId,stockInventory.itemId,item.itemName,stockInventory.unitId,unit.unitDesc,stockInventory.quantity,stockInventory.costPrice FROM單元INNER ( JOIN(item INNER JOIN stockInventory ON item.itemId = stockInventory.itemId)ON unit.unitId = stockInventory.unitId WHERE(((stockInventory.purchaseId)= 1)) )AS A; –

回答

1

附加SELECT部分​​

(SELECT COUNT(*) FROM A WHERE A.itemId>=itemId) as rowNo 

是一個單獨的查詢,不知道A

我認爲你必須保存你的原始查詢(=子查詢)作爲新的命名查詢,然後你可以在兩個SELECT部分​​中引用它。

SELECT A.*, 
     (SELECT COUNT(*) FROM mySubquery AS B WHERE B.itemId>=A.itemId) as rowNo 
FROM mySubquery AS A 

現在,它也變得更清晰,你需要兩個子查詢(A和B)的實例。

我希望你沒有太多的記錄,因爲性能可能會很差。但是,這不是本文的重點...

0

直接考慮加入rowNo子查詢的原始查詢:

SELECT (SELECT Count(*) FROM stockInventory AS sub 
     WHERE sub.itemId <= stockInventory.itemId) AS rowNo, 
     stockInventory.purchaseId, stockInventory.itemId, item.itemName, 
     stockInventory.unitId, unit.unitDesc, stockInventory.quantity, 
     stockInventory.costPrice 
FROM unit 
INNER JOIN (item 
INNER JOIN stockInventory 
ON item.itemId = stockInventory.itemId) 
ON unit.unitId = stockInventory.unitId 
WHERE (((stockInventory.purchaseId)=1))