Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

I am not sure where I am going wrong

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...