有一表号码          A           B        C
123           是          是        否
4567           否          否        是
5678            是          否        是
56667          是          否        是
我想查询出A、B、C列,其中任意两列为“是"的记录?谢谢了

解决方案 »

  1.   

    两种方式:
    1、case when
    2、replace
      

  2.   


    declare @T table 
    (号码 int,A varchar(2),B varchar(2),C varchar(2))
    insert into @T
    select 123,'是','是','否' union all
    select 4567,'否','否','是' union all
    select 5678,'是','否','是' union all
    select 56667,'是','否','是'--1.case when
    select * from @T where 
    case when A='是' then 1 else 0 end+
    case when B='是' then 1 else 0 end+
    case when C='是' then 1 else 0 end =2
    /*
    号码          A    B    C
    ----------- ---- ---- ----
    123         是    是    否
    5678        是    否    是
    56667       是    否    是
    */
    --2.replace
    select * from @T where len(A+B+C)-len(replace(A+B+C,'是',''))=2
    /*
    号码          A    B    C
    ----------- ---- ---- ----
    123         是    是    否
    5678        是    否    是
    56667       是    否    是
    */
      

  3.   


    declare @T table 
    (号码 int,A varchar(2),B varchar(2),C varchar(2))
    insert into @T
    select 123,'是','是','否' union all
    select 4567,'否','否','是' union all
    select 5678,'是','否','是' union all
    select 56667,'是','否','是'--1.case when
    select * from @T where case when A='是' then 1 else 0 end+case when B='是' then 1 else 0 end+case when C='是' then 1 else 0 end =2--2.replace
    select * from @T where len(A+B+C)-len(replace(A+B+C,'是',''))=2--3.or
    select * from @T where (A='是' and B='是' and C='否') or (A='是' and B='否' and C='是') or (A='否' and B='是' and C='是')--4.union all
    select * from @T where A='是' and B='是' and C='否' union all select * from @T where A='是' and B='否' and C='是' union all select * from @T where A='否' and B='是' and C='是'
      

  4.   

    select*
    from(
    select 号码=123,A='是',B='是',C='否'
    union all
    select 号码=4567,A='否',B='否',C='是'
    union all
    select 号码=5678,A='是',B='否',C='是'
    union all
    select 号码=56667,A='是',B='否',C='是') z
    where A+B+C like '%是%是%'
      

  5.   

    select * from 表
    where
       (A='是' or B='是' or C='否')
    or(A='是' or B='否' or C='是')
    or(A='否' or B='是' or C='是') 
      

  6.   


    不对,应该是select * from 表
    where
      (A='是' and B='是' and C='否')
    or(A='是' and B='否' and C='是')
    or(A='否' and B='是' and C='是')
      

  7.   


    select * from 表 
    where 
      ( case A when '是' then 1 else 0 end )
    + ( case B when '是' then 1 else 0 end ) 
    + ( case C when '是' then 1 else 0 end )
    = 2
      

  8.   


    USE tempdbcreate table mytest
    (
    num int,
    A nvarchar(1),
    B nvarchar(1),
    C nvarchar(1)
    )insert into mytest(num,A,B,C) values ('123','是','是','否')
    insert into mytest(num,A,B,C) values ('4567','否','否','是')
    insert into mytest(num,A,B,C) values ('5678','是','否','是')
    insert into mytest(num,A,B,C) values ('5667','是','否','是')
    select * from mytest as m where exists
    (
    SELECT 1 FROM 
    (SELECT num,
    case A
      when '是' then 1
      else 0 
    end as A,
    case B
      when '是' then 1
      else 0
    end as B,
    case C
      when '是' then 1
      else 0
    end as C 
    FROM mytest )as t 
    WHERE (t.A + t.B +t.C ) >=2 and t.num=m.num
    )
    /*
    123 是 是 否
    5678 是 否 是
    5667 是 否 是
    */