SQL change query to show orphans
Give the query:
SELECT
tblProducts.ID, tblProducts.views, tblProducts.productName, tblProducts.isForSale, tblProducts.isLimitedStock, tblProducts.stockCount, tblProducts.description, tblProducts.weightKG, tblProducts.basePrice, tblProducts.dateCreated, tblProductCats.catName,
(SELECT COUNT(*) FROM tblProductPrices WHERE productId = tblproducts.id) AS priceMods,
(SELECT COUNT(*) FROM tblcomments WHERE productId = tblproducts.ID) AS comments,
(SELECT COUNT(*) FROM tblProductImages WHERE productID = tblproducts.id) AS imageCount
FROM
tblProducts
INNER JOIN
tblProductCats ON tblProducts.categoryID = tblProductCats.ID
If the category ID for the product is 0 (tblProducts INNER JOIN tblProductCats ON tblProducts.categoryID = tblProdu开发者_如何学GoctCats.ID)
it will not return the product record, is there anyway to modify the query so it does return records with 0 as a value and shows the category name as 'Orphan'?
SELECT ...
ISNULL(tblProductCats.catName,'Orphan') AS catName ,
/*
Or possibly...
CASE
WHEN tblProducts.categoryID = 0
THEN 'Orphan'
ELSE tblProductCats.catName
END AS catName
*/
...
FROM tblProducts
LEFT OUTER JOIN tblProductCats
ON tblProducts.categoryID = tblProductCats.ID
精彩评论