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