from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = '张三' --这里怎么判断,为空时跳过,有数据时带进来查询
group by b.danwbh,b.dwmch,c.ywy
9 个解决方案
#1
declatre @str
set @str='张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = isnull(@str,c.ywy) --这里怎么判断,为空时跳过,有数据时带进来查询
group by b.danwbh,b.dwmch,c.ywy
#2
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '张三' or 1=1)
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '张三' or 1=1)
#3
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '张三' or c.ywy is null)
group by b.danwbh,b.dwmch,c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '张三' or c.ywy is null)
group by b.danwbh,b.dwmch,c.ywy
#4
declare @ywy as varchar(10)
set @ywy = '张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = isnull(@ywy, c.ywy)
group by b.danwbh,b.dwmch,c.ywy
#5
例如在程序中传入值是str=“张三”,则拼接sql字符串时:
String sql="select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '"+str+"' or '"+str+"'='')";
这样就可以了
String sql="select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '"+str+"' or '"+str+"'='')";
这样就可以了
#6
declatre @str
set @str='张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = isnull(@str,c.ywy) --当@str空的时候就查不出数据啦
group by b.danwbh,b.dwmch,c.ywy
set @str='张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = isnull(@str,c.ywy) --当@str空的时候就查不出数据啦
group by b.danwbh,b.dwmch,c.ywy
#7
你说的空是NULL还是''?
declare @ywy as varchar(10)
set @ywy = '张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = isnull(@ywy, c.ywy) or c.ywy = (case when @ywy = '' then c.ywy else @ywy end))
group by b.danwbh,b.dwmch,c.ywy
#8
declare @ywy as varchar(10)
set @ywy = '张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = (case when @ywy = '' or @ywy is null then c.ywy else @ywy end)
group by b.danwbh,b.dwmch,c.ywy
set @ywy = '张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = (case when @ywy = '' or @ywy is null then c.ywy else @ywy end)
group by b.danwbh,b.dwmch,c.ywy
#9
where a=@a or @a=''就行了。
#1
declatre @str
set @str='张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = isnull(@str,c.ywy) --这里怎么判断,为空时跳过,有数据时带进来查询
group by b.danwbh,b.dwmch,c.ywy
#2
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '张三' or 1=1)
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '张三' or 1=1)
#3
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '张三' or c.ywy is null)
group by b.danwbh,b.dwmch,c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '张三' or c.ywy is null)
group by b.danwbh,b.dwmch,c.ywy
#4
declare @ywy as varchar(10)
set @ywy = '张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = isnull(@ywy, c.ywy)
group by b.danwbh,b.dwmch,c.ywy
#5
例如在程序中传入值是str=“张三”,则拼接sql字符串时:
String sql="select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '"+str+"' or '"+str+"'='')";
这样就可以了
String sql="select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = '"+str+"' or '"+str+"'='')";
这样就可以了
#6
declatre @str
set @str='张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = isnull(@str,c.ywy) --当@str空的时候就查不出数据啦
group by b.danwbh,b.dwmch,c.ywy
set @str='张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = isnull(@str,c.ywy) --当@str空的时候就查不出数据啦
group by b.danwbh,b.dwmch,c.ywy
#7
你说的空是NULL还是''?
declare @ywy as varchar(10)
set @ywy = '张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and (c.ywy = isnull(@ywy, c.ywy) or c.ywy = (case when @ywy = '' then c.ywy else @ywy end))
group by b.danwbh,b.dwmch,c.ywy
#8
declare @ywy as varchar(10)
set @ywy = '张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = (case when @ywy = '' or @ywy is null then c.ywy else @ywy end)
group by b.danwbh,b.dwmch,c.ywy
set @ywy = '张三'
select b.danwbh,b.dwmch,sum(a.ml) as ml, c.ywy
from a, b, c
where a.dwbh=b.dwbh
and a.djbh = c.djbh
and a.rq>='2011-04-14'
and a.rq<='2011-04-14'
and c.ywy = (case when @ywy = '' or @ywy is null then c.ywy else @ywy end)
group by b.danwbh,b.dwmch,c.ywy
#9
where a=@a or @a=''就行了。