根据条件选择列 [英] Select column based on condition

查看:74
本文介绍了根据条件选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友,



我想根据条件选择栏目

我有一张像
$ 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


这篇关于根据条件选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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