现有一张表A 
有字段 itemCode 和groupNo
itemCode表中数据唯一,
Groupno表中数据不唯一,而且有可能是0或者NULL
如何根据itemCode和groupNo查出满足条件的数据
当GroupNo为0或者为NULL只会查出itmeCode所对应的一条记录,
否则查出groupNo,ItemCode所对应的所有记录
请问此sql语句怎么写?

解决方案 »

  1.   

    select groupNo,ItemCode from table
    where (groupNo = 值 or isnull(groupNo,0)=0)
      

  2.   

    楼主可否讲清楚点啊,表结构
    是不是这个意思?
    select * from tb where groupno <>0 and groupno is not null
      

  3.   

    select groupNo,ItemCode from table
    where groupNo=
       (case when isnull(groupNo,0)=0 then '值' else groupNo end)
      

  4.   

    select groupNo,ItemCode from table
    where 
    ItemCode=@ItemCode
    and (isnull(groupNo,0)=0 or groupNo=@groupNo)
      

  5.   

    select groupNo,ItemCode from A
    where  
    ItemCode=@ItemCode and (isnull(groupNo,0)=0 or groupNo=@groupNo)
      

  6.   

    你们都没仔细看,楼主需要groupno为0和NULL时候的
    ItemCode数据;同时也需要groupno不为0和不为NULL时候的 所有数据。select groupNo,ItemCode from table where groupno <>0 and groupno is not null
    --查询出 groupno 不为 0 和 null时候的数据select ItemCode form table
    --不考虑 groupNo 单独查询出ItemCode 那么完整的语句就是:
    select groupNo,ItemCode from table where groupno <>0 and groupno is not null
    union
    select ItemCode form table
    --将两条记录合并,采用字段同值合并,防止ItemCode重复。
      

  7.   

    看来我悲剧了 楼主 
    itemCode表中数据唯一,
    Groupno表中数据不唯一,而且有可能是0或者NULL
    ++itemCode  表中数据唯一,
    Groupno  表中数据不唯一,而且有可能是0或者NULL
      

  8.   

    楼主描述有问题我感觉楼主 是
    想表达
    groupno不为0或者null的时候
    select * from tb where itemCode='值' and groupno='值'
    groupno为0或者null的时候
    select * from tb where itemCode='值
      

  9.   

    select * from tb 
    where 主键 in
    (
        select 主键 from tb where groupno is null or groupno !=0
    ) and itemCode='值' and groupno='值'union select * from tb 
    where 主键 in
    (
        select 主键 from tb where groupno is null or groupno =0
    ) and itemCode='值'
      

  10.   

    你的需求有问题,GroupNo表中数据不唯一,也就是说有可能很多是0 或者null,那怎么查所对应的itemCode的一条记录???除非还有别的唯一字段,否则不可能就对应一条itemCode记录
    按照楼主的意思
    select itemCode,GroupNo from A where GroupNo =0 or GroupNo is null
    --不过这样取出来的itemCode 肯定不会是一条,除非你表里就只有一条。
    --是不是还有别的唯一字段???
    当GroupNo不为0或null的时候
    select itemCode,groupNo from A where GroupNo <> 0 and 
    GroupNo is not null
    --如果按照楼主这个需求那么这两个条件的话
    也就是
    select itemCode,GroupNo from A