case when then php,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. 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

回答1:

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

回答2:

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. 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.

回答3:

For a start the first select has 6 columns and the second has 4 columns. Perhaps make both have the same number of columns (adding nulls?).

回答4:

I ended up leaving the common properties from the SELECT queries and making a second SELECT query later on in the page. I used a php IF command to call for different scripts depending on the first SELECT query, the scripts contained the second SELECT query.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在SQL中,SELECT CASE WHEN THEN语句用于在查询结果中根据条件返回不同的值。它可以根据给定的条件判断并返回相应的结果。 举个例子,可以使用SELECT CASE WHEN THEN语句来判断两个表之间的匹配情况。使用EXISTS方法,可以检查在tbl_a和tbl_b中是否存在具有相同键值的记录,如果存在则返回"matched",如果不存在则返回"unmatched"。具体语法如下所示: SELECT keycol, CASE WHEN EXISTS (SELECT * FROM tbl_b WHERE tbl_a.keycol = tbl_b.keycol) THEN 'matched' ELSE 'unmatched' END AS label FROM tbl_a; 另外,可以使用SELECT CASE WHEN THEN语句来解决复杂的条件问题。例如,在studentclass表中,我们想根据条件来选择主要班级。如果一个学生只有一个班级,则返回最大的class_id;如果一个学生有多个班级,并且其中有一个主要班级,则返回主要班级的class_id;否则返回null。具体语法如下所示: SELECT std_id, CASE WHEN COUNT(*) = 1 THEN MAX(class_id) ELSE MAX(CASE WHEN main_class_flg = 'y' THEN class_id ELSE NULL END) END AS main_class FROM studentclass GROUP BY std_id; 通过使用SELECT CASE WHEN THEN语句,我们可以根据不同的条件返回不同的结果,从而灵活地进行查询和处理数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [sql语句中case when及select case when的用法](https://blog.csdn.net/wh_07/article/details/103292280)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值