根据条件选择列

编程入门 行业动态 更新时间:2024-10-11 15:22:29
本文介绍了根据条件选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨朋友, 我想根据条件选择栏目 我有一张像 $ b $的桌子b 表: ID~Phone1~Phone2~Phone3~phone4~Phone5 1~99~88~0~0~77 现在我想只选择列及其值大于0即 预期结果: ID~Phone1~Phone2~Phone5 1~99~88~77 如果所有的phonenumber都是< 0然后应该单独返回ID。 问候, RK

Hi Friends, I want to select columns based on condition I have a table like Table: ID~Phone1~Phone2~Phone3~Phone4~Phone5 1~99~88~0~0~77 Now I want to select only the column and its value that is greater than 0 that is Expected result: ID~Phone1~Phone2~Phone5 1~99~88~77 If all phonenumber is < 0 then should return ID alone. Regards, RK

推荐答案

嗨朋友, 这是我试过的, Hi Friends, Here is what I have tried, DECLARE @query VARCHAR(1000)=null; SET @query = 'ID'; IF((select phone1 from tablename where id = 1459) > 0) SET @query += ',phone1'; IF((select phone2 from tablename where id = 1459) > 0) SET @query += ',phone2'; .. .. .. IF (@query IS NOT NULL) exec('SELECT '+@query+' FROM tablename WHERE id = 1459');

我不知道上面的解决方案是否是最佳的,但它给了我预期的答案。所以我选择了上面的方法。 希望这也可以帮助别人。 问候, RK

I do not know whether the above solution is optimal or not but it gives me expected answer.So I selected the above method. Hope this may help someone too. Regards, RK

更多推荐

根据条件选择列

本文发布于:2023-11-30 06:21:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!