请问这个sql语句怎么写呀?

[复制链接]
查看11 | 回复3 | 2011-7-22 11:11:04 | 显示全部楼层 |阅读模式
现有一张表A
有字段 itemCode 和groupNo
itemCode表中数据唯一,
Groupno表中数据不唯一,而且有可能是0或者NULL
如何根据itemCode和groupNo查出满足条件的数据
当GroupNo为0或者为NULL只会查出itmeCode所对应的一条记录,
否则查出groupNo,ItemCode所对应的所有记录
请问此sql语句怎么写?

回复

使用道具 举报

千问 | 2011-7-22 11:11:04 | 显示全部楼层
select distinct itemcode,isnull(groupno,0)as groupnofrom Awhere 把Null的值以0显示,并Distint去重,不过,对于其他值也会去重,性能差一点,稳妥的做法是select distinct itemcode,isnull(groupno,0)as groupnofrom Awhere isnull(groupno,0)=0union allselect distinct itemcode,groupnofrom Awhere isnull(groupno,0)0...
回复

使用道具 举报

千问 | 2011-7-22 11:11:04 | 显示全部楼层
select top 1 groupNo,ItemCode from 表Awhere isnull(groupNo,0)=0union allselect groupNo,ItemCode from 表Awhere isnull(groupNo,0)0...
回复

使用道具 举报

千问 | 2011-7-22 11:11:04 | 显示全部楼层
select * from a where itemCode = '111' and (groupNo='111' or groupNo=0 or groupNo is null)...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行