比如如下的SQL语句:
select sum(f1),f2 from (
select a.a1 as f1, a.a2 as f2, a.a3 as f3 from a
left join select b.b1 as f1,b.b2 as f2, b.b3 as f3 from b
union all
select c.c1 as f1,c.c2 as f2, c.c3 as f3 from c where c.c1 < 100
) group by f2
要程序实现给查询语句添加条件 a.a1 > 10、b.b2 = 5 、c.c1 < 100达到下面的效果:
select sum(f1),f2 from (
select a.a1 as f1, a.a2 as f2, a.a3 as f3 from a
left join select b.b1 as f1,b.b2 as f2, b.b3 as f3 from b
where a.a1 > 10 and b.b2 = 5
union all
select c.c1 as f1,c.c2 as f2, c.c3 as f3 from c where c.c1 < 100
) group by f2
怎么实现这样的效果请指点一下。
用存储过程,设置条件。
首先存储过程里声明变量。
declare @a1 int
declare @b2 int
declare @c1 int
//现在读取a.a1赋值给@a1
select @a1=a1 from a
select @b2=b2 from b
select @c1=c1 from c
//然后就是条件判断
if (@a1 > 10 and @b2 = 5 and @c1<100)
begin
//执行你所需的sql语句
end
else
begin
//执行你所需的sql
end
具体存储过程书写规范请自行查阅相关资料,这里只是提供思路。
你用的什么程序,可以用存储过程啊,或者直接在程序中作为变量也可以啊,得具体点
这种问题要自己思考。。。
你用的什么框架访问的数据库,如果是自己写SQL,可以直接拼接,如果是其它框架应该有对应的方法