简体   繁体   English

然后选择案例(选择)

[英]SELECT CASE WHEN THEN (SELECT)

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. 因此,如果我的产品应该是一本书,我希望它查找UPC和艺术家的正常产品,但这些细节无关紧要,对于另一种产品,我想要一组完全不同的结果。

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 我不确定我哪里出错了

You Could try the other format for the case statement 您可以尝试使用case语句的其他格式

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. 有关更多信息,请参见http://msdn.microsoft.com/en-us/library/ms181765.aspx

You should avoid using nested selects and I would go as far to say you should never use them in the actual select part of your statement. 你应该避免使用嵌套选择,我会尽量说你不应该在你的语句的实际选择部分使用它们。 You will be running that select for each row that is returned. 您将为返回的每一行运行该选择。 This is a really expensive operation. 这是一项非常昂贵的操作。 Rather use joins. 而是使用连接。 It is much more readable and the performance is much better. 它更具可读性,性能更好。

In your case the query below should help. 在您的情况下,以下查询应该有所帮助。 Note the cases statement is still there, but now it is a simple compare operation. 注意case语句仍然存在,但现在它是一个简单的比较操作。

select
    p.product_id,
    p.type_id,
    p.product_name,
    p.type,
    case p.type_id when 10 then (CONCAT_WS(' ' , first_name, middle_name, last_name )) else (null) end artistC
from
    Product p

    inner join Product_Type pt on
        pt.type_id = p.type_id

    left join Product_ArtistAuthor paa on
        paa.artist_id = p.artist_id
where
    p.product_id = $pid

I used a left join since I don't know the business logic. 我使用左连接,因为我不知道业务逻辑。

For a start the first select has 6 columns and the second has 4 columns. 首先,第一个选择有6列,第二个选择有4列。 Perhaps make both have the same number of columns (adding nulls?). 或许使两者具有相同的列数(添加空值?)。

I ended up leaving the common properties from the SELECT queries and making a second SELECT query later on in the page. 我最终从SELECT查询中保留了公共属性,并在页面中稍后进行了第二次SELECT查询。 I used a php IF command to call for different scripts depending on the first SELECT query, the scripts contained the second SELECT query. 我使用php IF命令根据第一个SELECT查询调用不同的脚本,脚本包含第二个SELECT查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM