where 子语句中,有下列要求:如果A字段为1,则y=1,如果A字段为2,则x=1,如果A字段为3,则y=1同时x=1注:A字段为select from 表中的某字段,y和x都是where子语句中的检索条件求写法
解决方案 »
- 怎么样实现淘宝的这种价格存储方式。
- 又是怪问题!
- 数据库进行分区表问题
- 求SQL SERVER学习方面的书籍推荐 !!
- 我想实现下面的排序,有办法吗
- 求一简单插入完整存储过程,表new中有title,author,rq,content四个字段,在提交页面有相应的四个提交文本框,名字与字段名字相同。
- 斑竹,为什么我的散分帖给删除了?什么地方得罪了?讨论有分!
- MS SQL中自定义函数和存储过程的问题
- 急!急!急!sqlserver转为ccess后表没有主建,字符型的字段也全部默认不为空,怎么办?
- 关于触发器中的插入。。。。
- 求教,能不能在从数据库读取数据的时候检查数据的合法性?
- 求sql查询语句
insert into @table
select 1,1,1 union all
select 2,1,2 union all
select 3,0,1select * from @table
/*
A X Y
----------- ----------- -----------
1 1 1
2 1 2
3 0 1
*/
where w = 'xxx'
and z = 5
and ......即我发帖要追加的where条件,根据A值的不同追加不同的条件
declare @table table (A int,X int,Y int)
insert into @table
select 1,1,1 union all
select 2,1,2 union all
select 3,0,1declare @A int
set @A=1if(@A=1)
select * from @table where Y=1
if(@A=2)
select * from @table where X=1
if(@A=3)
select * from @table where X=1 AND Y=1
declare @sql varchar(8000)
set @sql='select * from tabel1 where w = ''xxx'' and z = 5'
+case @A when 1 then ' and Y=1' when 2 then 'and X=1'
when 3 then 'and X=1 AND Y=1' END
exec(@sql)
set @sql='select * from tabel1 where w = ''xxx'' and z = 5 '
+case @A when 1 then ' and Y=1' when 2 then ' and X=1'
when 3 then ' and X=1 AND Y=1' END
exec(@sql)
insert into @table
select 1,1,1 union all
select 2,1,2 union all
select 3,0,1declare @A int
set @A=3 --改成1 或是2 运行可以看结果select * from @table
where
Y=isnull(case @A when 1 then 1 when 3 then 1 else null end,Y)
and X=isnull(case @A when 2 then 1 when 3 then 1 else null end,X)
declare @sql nvarchar(4000)set @sql ='select * from TableName where case A when 1 then y = 1 when 2 then x=1 else y=1 and x=1 end'exec @sql