字段 aa bb
1 1
0 0
0 1
0 0
要求实现:如果bb的值=1 ,加入条件查询aa=1的数据如果bb的值=0,不加入查询aa的条件
解决方案 »
- BULK INSERT 导入文本的问题
- 请教大家计算Category下面Products的数量
- 请各位老大帮帮忙!!!我想将数据库里的几个表中的数据导出,并加密?就这些分了,全给了
- SQL2005 提示对象名 “XXX”无效!请高手解决
- 产品入库时,同时修改库存数量的问题
- 关于SQL Server服务器硬件配置的问题,现在有一个SQL Server用的服务器,并发用户>50(一直维持这么多的并发用户,并发连接大约维持在200
- 【Kill的用法】如何Kill自己的连接
- 如何求出两个日期之间的所有记录?
- 一个select 语句的问题
- 弱弱问一个问题,如何找出表中不同商品有效期最长的记录?
- SQL复杂问题
- 求一条SQL语句?
bb = 1 and aa = 1
or bb = 0
aa bb
1 1
0 0
0 0
1 1 李小姐
0 0 张小姐
0 1 陈小姐
0 0 易小姐
这样问如果bb=1 那么就输出 aa=1的值哪果BB=0 那么就输出 所有的值
case bb when 1 then (select aa from tb where aa=1)
when 0 then ...end
from
tb
select * from table1
where bb = 1 and aa = 1
union all
select * from table1
where bb = 0
消息 156,级别 15,状态 1,第 3 行
关键字 'end' 附近有语法错误。
begin
if(bb=1)
select * from tb where aa=1
else
select * from tb
end
from tb1
where (case when bb=1 then aa else 1 end)=1
set @bb2=1
--set @bb2=0select *
from tb1
where (case when @bb2=1 then aa else 1 end)=1
where (
(bb = 1 and aa = 1) or
(bb <> 1)
)
if object_id('test') is not null
drop table test
gocreate table test(aa int, bb int, cc nvarchar(5))
goinsert into test
select 1, 1, '李小姐' union all
select 0, 0, '张小姐' union all
select 0, 1, '陈小姐' union all
select 0, 0, '易小姐'
goselect *
from test
where bb = 1 and aa =1 or bb =0
goif object_id('test') is not null
drop table test
go/*
aa bb cc
----------- ----------- -----
1 1 李小姐
0 0 张小姐
0 0 易小姐
*/
where (bb=1 and aa =1) or bb=0
drop table test
create table test(aa int, bb int, cc nvarchar(5))
go
insert into test
select 1, 1, '李小姐' union all
select 0, 0, '张小姐' union all
select 0, 1, '陈小姐' union all
select 0, 0, '易小姐'
go
--SQL:
select * from test
where aa = (case when bb = 1 then 1 else aa end)
/*
aa bb cc
1 1 李小姐
0 0 张小姐
0 0 易小姐
*/