我有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'
显然我犯了错误..
答案 0 :(得分: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
表
答案 1 :(得分: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
答案 2 :(得分:0)
f是SELECT COUNT(b.stockID)结果的别名...... 所以f没有collumn