如何查询 一条记录里 不为空的 数据

hwf0217 2017-11-05 09:40:41

比如上表,我想显示路东泵站里不为NULL的数据 ,为空的字段都过滤掉
当然路东只是举例,可能还有其他记录,不为NUll的字段也许不一致
...全文
669 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hwf0217 2017-11-10
  • 打赏
  • 举报
回复
引用 11 楼 shadowpj 的回复:
直接Select isnull(工程名称,'')+isnull(工程地点,'')+isnull(工程造价,'')
+isnull(人员,'')+.......
From Table 是不行的哦,比如工程地点是空那么显示
工程名称 空列 工程造价 人员
东路工程 空值 2500 张三
估计还是得用我的动态指定列名
declare @tmp varchar(100), --临时记录需要查询字段串
@gc varchar(20) --每条记录空的字段不一样,只能一条判断,暂且已不重复工程名称做条件
set @gc='东路工程' --谢塘改造工程 东路工程
set @tmp='工程名称'

if (select IsNull(工程地点,'') from a where 工程名称=@gc)<>''
set @tmp=@tmp+',工程地点'

if (select IsNull(工程造价,'') from a where 工程名称=@gc)<>''
set @tmp=@tmp+',工程造价'

if (select IsNull(工程人员,'') from a where 工程名称=@gc)<>''
set @tmp=@tmp+',工程人员'

if (select IsNull(设备,'') from a where 工程名称=@gc)<>''
set @tmp=@tmp+',设备'

if (select IsNull(备注,'') from a where 工程名称=@gc)<>''
set @tmp=@tmp+',备注'

--后面还有字段自己顺着加判断。。。。
exec('select '+@tmp+' from a where 工程名称='''+@gc+'''')





你的方法符合客户的需求,谢谢了,我软件代码里今天去试试
hwf0217 2017-11-10
  • 打赏
  • 举报
回复
引用 11 楼 shadowpj 的回复:
直接Select isnull(工程名称,'')+isnull(工程地点,'')+isnull(工程造价,'') +isnull(人员,'')+....... From Table 是不行的哦,比如工程地点是空那么显示 工程名称 空列 工程造价 人员 东路工程 空值 2500 张三 估计还是得用我的动态指定列名 declare @tmp varchar(100), --临时记录需要查询字段串 @gc varchar(20) --每条记录空的字段不一样,只能一条判断,暂且已不重复工程名称做条件 set @gc='东路工程' --谢塘改造工程 东路工程 set @tmp='工程名称' if (select IsNull(工程地点,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程地点' if (select IsNull(工程造价,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程造价' if (select IsNull(工程人员,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程人员' if (select IsNull(设备,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',设备' if (select IsNull(备注,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',备注' --后面还有字段自己顺着加判断。。。。 exec('select '+@tmp+' from a where 工程名称='''+@gc+'''')
我去试下
引用 11 楼 shadowpj 的回复:
直接Select isnull(工程名称,'')+isnull(工程地点,'')+isnull(工程造价,'') +isnull(人员,'')+....... From Table 是不行的哦,比如工程地点是空那么显示 工程名称 空列 工程造价 人员 东路工程 空值 2500 张三 估计还是得用我的动态指定列名 declare @tmp varchar(100), --临时记录需要查询字段串 @gc varchar(20) --每条记录空的字段不一样,只能一条判断,暂且已不重复工程名称做条件 set @gc='东路工程' --谢塘改造工程 东路工程 set @tmp='工程名称' if (select IsNull(工程地点,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程地点' if (select IsNull(工程造价,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程造价' if (select IsNull(工程人员,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程人员' if (select IsNull(设备,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',设备' if (select IsNull(备注,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',备注' --后面还有字段自己顺着加判断。。。。 exec('select '+@tmp+' from a where 工程名称='''+@gc+'''')
我去试下,主要列名我也是不知道的,都是用户动态加进去的,如果难实现,列名我就全部给显示算了,用户也能接受
shadowpj 2017-11-07
  • 打赏
  • 举报
回复
直接Select isnull(工程名称,'')+isnull(工程地点,'')+isnull(工程造价,'') +isnull(人员,'')+....... From Table 是不行的哦,比如工程地点是空那么显示 工程名称 空列 工程造价 人员 东路工程 空值 2500 张三 估计还是得用我的动态指定列名 declare @tmp varchar(100), --临时记录需要查询字段串 @gc varchar(20) --每条记录空的字段不一样,只能一条判断,暂且已不重复工程名称做条件 set @gc='东路工程' --谢塘改造工程 东路工程 set @tmp='工程名称' if (select IsNull(工程地点,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程地点' if (select IsNull(工程造价,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程造价' if (select IsNull(工程人员,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程人员' if (select IsNull(设备,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',设备' if (select IsNull(备注,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',备注' --后面还有字段自己顺着加判断。。。。 exec('select '+@tmp+' from a where 工程名称='''+@gc+'''')
wwfxgm 2017-11-07
  • 打赏
  • 举报
回复
引用 5 楼 shadowpj 的回复:
declare @tmp varchar(100), --临时记录需要查询字段串 @gc varchar(20) --每条记录空的字段不一样,只能一条判断,暂且已不重复工程名称做条件 set @gc='东路工程' --谢塘改造工程 东路工程 set @tmp='工程名称' if (select IsNull(工程地点,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程地点' if (select IsNull(工程造价,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程造价' if (select IsNull(工程人员,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程人员' if (select IsNull(设备,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',设备' if (select IsNull(备注,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',备注' --后面还有字段自己顺着加判断。。。。 exec('select '+@tmp+' from a where 工程名称='''+@gc+'''')
楼主这个测试了没有,应该可以实现呀。
shadowpj 2017-11-06
  • 打赏
  • 举报
回复
declare @tmp varchar(100), --临时记录需要查询字段串 @gc varchar(20) --每条记录空的字段不一样,只能一条判断,暂且已不重复工程名称做条件 set @gc='东路工程' --谢塘改造工程 东路工程 set @tmp='工程名称' if (select IsNull(工程地点,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程地点' if (select IsNull(工程造价,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程造价' if (select IsNull(工程人员,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',工程人员' if (select IsNull(设备,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',设备' if (select IsNull(备注,'') from a where 工程名称=@gc)<>'' set @tmp=@tmp+',备注' --后面还有字段自己顺着加判断。。。。 exec('select '+@tmp+' from a where 工程名称='''+@gc+'''')
听雨停了 2017-11-06
  • 打赏
  • 举报
回复
你说的这个需求是没有办法实现的,很简单的道理,假如表一共十个字段,第一行数据有两个为null的字段,第二行有三个为null的字段,第三行有8个为null的字段,那么请问查询出来的数据,一个8行,一个7行,一个2行,怎么合并到一起显示?
hwf0217 2017-11-06
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:
select case when col1 is null then 0 else 1 end + case when col2 is null then 0 else 1 end + 。。。 from t
我要显示对应不为空记录的列名的
hwf0217 2017-11-06
  • 打赏
  • 举报
回复
我想把后面两张图里,数据库里显示为NULL的列名不显示
如下图
hwf0217 2017-11-06
  • 打赏
  • 举报
回复

效果就是上面图显示的, 点全部显示按钮后,显示数据库里所有字段(列名)
点单个显示后,只显示跟这个泵站有关的不为空的字段的列名
吉普赛的歌 2017-11-06
  • 打赏
  • 举报
回复
只有一列不为空还是所有的列都不为空?
  • 打赏
  • 举报
回复
是不是要把全部null的某一列 不显示
卖水果的net 2017-11-06
  • 打赏
  • 举报
回复
select case when col1 is null then 0 else 1 end + case when col2 is null then 0 else 1 end + 。。。 from t
顺势而为1 2017-11-06
  • 打赏
  • 举报
回复


Select isnull(工程名称,'')+isnull(工程地点,'')+isnull(工程造价,'')
       +isnull(人员,'')+.......
From Table

590

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧