开发者

SQL change query to show orphans

开发者 https://www.devze.com 2023-01-13 19:22 出处:网络
Give the query: SELECT tblProducts.ID, tblProducts.views, tblProducts.productName, tblProducts.isForSale, tblProducts.isLimitedStock, tblProducts.stockCount, tblProducts.description, tblProducts.weig

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号