有一表号码 A B C
123 是 是 否
4567 否 否 是
5678 是 否 是
56667 是 否 是
我想查询出A、B、C列,其中任意两列为“是"的记录?谢谢了
123 是 是 否
4567 否 否 是
5678 是 否 是
56667 是 否 是
我想查询出A、B、C列,其中任意两列为“是"的记录?谢谢了
解决方案 »
- 如何把一行的某列的值赋给另一行的同列
- 呼叫小三,修改SQL
- 请问这个SQL语句有错吗?
- select * into from 记录顺序怎么不一致? 万分火急!
- 请教出现超出了存储过程、函数、触发器或视图的最大嵌套层数的问题,谢谢!!!
- 一台机器安装了sql server2000与sql server2005,应该如何访问?
- 急求:大约10万元左右的服务器建议,要求(能满足大容量数据存取要求\可扩性要好\稳定性\限国内产商)
- 行追加到列?
- 紧急问题!!!
- 设计人员资料信息表,“性别”一栏如何处理? 数据库设计学习中
- 求,收缩数据库。2T的盘,现在数据库文件已经达到1.8T了,只剩下30G了,急!!!!
- Access链接服务器与txt链接服务器同时访问,冲突
1、case when
2、replace
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 是 否 是
*/
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='是'
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 '%是%是%'
where
(A='是' or B='是' or C='否')
or(A='是' or B='否' or C='是')
or(A='否' or B='是' or C='是')
不对,应该是select * from 表
where
(A='是' and B='是' and C='否')
or(A='是' and B='否' and C='是')
or(A='否' and B='是' and C='是')
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
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 是 否 是
*/