选择案例时(选择)

2021-12-19 00:00:00 select mysql case

我正在尝试根据产品类型为产品选择一组不同的结果.因此,如果我的产品应该是一本书,我希望它查找普通产品的 UPC 和艺术家,但这些细节无关紧要,而对于另一个产品,我想要一组完全不同的结果.

I am trying to select a different set of results for a product depending on a product type. So if my product should be a book I want it to look up the UPC and Artist for a normal product these details are however irrelevant and for another product I would want a completely different set of results.

SELECT CASE Product.type_id
    WHEN 10 THEN (
        SELECT 
        Product.product_id, 
        Product.type_id, 
        Product.product_name, 
        Product.UPC,
        Product_Type.type,
        CONCAT_WS(' ' , first_name, middle_name, last_name ) AS artistC 
        FROM Product, Product_Type, Product_ArtistAuthor 
        WHERE Product.type_id = Product_Type.type_id 
        AND Product.product_id = $pid
        AND Product.artist_id = Product_ArtistAuthor.artist_id
    )
    ELSE (
        SELECT 
        Product.product_id, 
        Product.type_id, 
        Product.product_name,
        Product_Type.type 
        FROM Product, Product_Type 
        WHERE Product.type_id = Product_Type.type_id 
        AND Product.product_id = $pid
    )
END
FROM Product 
WHERE Product.product_id = $pid

我不确定我哪里出错了

推荐答案

你可以试试 case 语句的其他格式

You Could try the other format for the case statement

CASE WHEN Product.type_id = 10
THEN
(
  Select Statement
)
ELSE
(
  Other select statement

)  
END
FROM Product 
WHERE Product.product_id = $pid

参见 http://msdn.microsoft.com/en-us/library/ms181765.aspx 了解更多信息.

See http://msdn.microsoft.com/en-us/library/ms181765.aspx for more information.

相关文章