0

我有 stockcards 和 stockcategories 表。

我想显示类别并显示在此类别下打开了多少张股票卡。

SELECT a.stockCatID, a.stockCatName, f.nProductsInside 
FROM stockcategories a 
LEFT JOIN 
        (
            SELECT COUNT(b.stockID) AS nProductsInside 
            FROM stockcards b 
        ) 
        f ON a.stockCatID = f.stockCatID 

好吧,它返回#1054 - Unknown column 'f.stockCatID' in 'on clause' 显然我犯了一个错误..

4

3 回答 3

0

尝试类似的东西:

SELECT a.stockCatID, a.stockCatName, COUNT(b.stockID) AS nProductsInside
FROM stockcategories a 
LEFT JOIN stockcards b ON a.stockCatID = b.stockCatID
GROUP BY a.id

a.id中的主键在哪里stockcategories

于 2012-05-07T16:52:07.143 回答
0
SELECT a.stockCatID, a.stockCatName, COUNT(b.stockID) as nProductsInside 
FROM stockcategories a 
LEFT JOIN stockcards b ON a.stockCatID = b.stockCatID 
group by a.stockCatID

你也可以这样做(未经验证)你为了坚持你的查询

SELECT a.stockCatID, a.stockCatName, f.nProductsInside 
FROM stockcategories a 
LEFT JOIN 
(
    SELECT COUNT(b.stockID) AS nProductsInside, stockCatID
    FROM stockcards b 
) 
f ON a.stockCatID = f.stockCatID 
于 2012-05-07T16:52:10.950 回答
0

f 是 SELECT COUNT(b.stockID)... 结果的别名,所以 f 没有列

于 2012-05-07T16:56:38.990 回答