1

我在 Access 2010 (accdb) 上有这个查询,它工作得很好:

SELECT b.category_name, a.item_name,  
(SELECT COUNT(*) FROM tbl_stock_receiving AS x WHERE x.safe_stock_id = a.ID) AS received,   
(SELECT COUNT(*) FROM tbl_stock_issuance AS y WHERE y.stock_receiving_id = a.ID) AS issued,
(received-issued) AS on_hand, a.safe_stock  
FROM tbl_safe_stock AS a INNER JOIN tbl_category AS b
ON a.category_id = b.id
ORDER BY a.item_name;

现在,我需要修改它以包含一个简单的 WHERE 语句

...
ON a.category_id = b.id
WHERE a.safe_stock > on_hand
ORDER BY a.item_name;
...

当我运行查询时,Access 不断弹出一个参数值

手上

on_hand 是一个别名,如您所见

(received-issued) AS on_hand

我的查询可能有什么问题?

4

1 回答 1

2

您已定义on_hand输出列的名称,但 SQL 解析器不够聪明,无法返回查询并“逆向工程”从何on_hand而来。因此,您需要使用类似的东西WHERE a.safe_stock > (received - issued),但它们也是输出列的别名。

所以试着把整个事情包装成一个子查询,然后应用WHEREandORDER BY子句:

SELECT * FROM
(
    SELECT b.category_name, a.item_name,  
    (SELECT COUNT(*) FROM tbl_stock_receiving AS x WHERE x.safe_stock_id = a.ID) AS received,   
    (SELECT COUNT(*) FROM tbl_stock_issuance AS y WHERE y.stock_receiving_id = a.ID) AS issued,
    (received-issued) AS on_hand, a.safe_stock  
    FROM tbl_safe_stock AS a INNER JOIN tbl_category AS b
    ON a.category_id = b.id
)
WHERE safe_stock > on_hand
ORDER BY item_name

编辑

上面的建议导致“查询太复杂”错误,所以我的下一个建议是将原始查询保存为 [StockCheckBaseQuery] 然后执行

SELECT * FROM StockCheckBaseQuery WHERE safe_stock > on_hand ORDER BY item_name

这似乎是成功的。

于 2013-05-03T09:05:36.703 回答