最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

根据条件选择列

SEO心得admin60浏览0评论
本文介绍了根据条件选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨朋友, 我想根据条件选择栏目 我有一张像 $ 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

发布评论

评论列表(0)

  1. 暂无评论