然后选择情况(选择) [英] SELECT CASE WHEN THEN (SELECT)

查看:98
本文介绍了然后选择情况(选择)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据产品类型为产品选择不同的结果集. 因此,如果我的产品应该是一本书,我希望它查找正常产品的UPC和Artist,那么这些细节就无关紧要了,而对于另一种产品,我希望得到一套完全不同的结果.

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

推荐答案

您可以尝试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 了解更多信息.

这篇关于然后选择情况(选择)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆