多项查询选择如何判别?急急!

PowerSoft-001 2003-08-20 09:31:19
我在做一个asp页面文件。上面有四个类别查询列表框。
分别是:class_1,class_2,class_3,class_4
它们是联动的。现在要把选择的条件传给一个存储过程,就会出现这一种情况:
用户可能只选了class_1,class_2,后面两个没有选(有默认不选的状态)。
也有可能用户选了3个和四个条件全选。
那么要在查询的sql语句的条件当中加以区别,如:
select * from a where a.x1=class_1 and a.x2=class_2

select * from a where a.x1=class_1 and a.x2=class_2 and a.x3=class_3

我想了一下,这种搭配组合可能有16种,难道要把16种组合一一列出加以区别不成?
如果选择条件再多一点呢?

请教各位高手有何良好的解决办法?

谢谢!
...全文
33 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-08-20
  • 打赏
  • 举报
回复
select * from a where a.x1=isnull(@class_1,a.x1) and a.x2=isnull(@class_2,a.x2)
and a.x3=isnull(@class_3,a.x3)

这样如果没有给参数就会被忽略。
dafu71 2003-08-20
  • 打赏
  • 举报
回复
<%
dim str
str="select * from a where 1=1"
if 选择1 then
str=str & " and a.x1=class_1"
end if
if 选择2 then
str=str & " and a.x2=class_2"
end if
if 选择3 then
str=str & " and a.x3=class_3"
end if
if 选择4 then
str=str & " and a.x4=class_4"
end if
%>

CrazyFor 2003-08-20
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql=' select * from a where 1=1 '
if class_1<>''
@sql=@sql=' and a.x1=class_1 '
if class_2<>''
@sql=@sql=' and a.x2=class_2 '
.....
exec(@sql)
amtyuranus 2003-08-20
  • 打赏
  • 举报
回复
只不过把一条sql 语句的条件根据他是否选择加一判断

那刚好加了就加一个条件就可以了
txlicenhe 2003-08-20
  • 打赏
  • 举报
回复
if 选择1
cond1 = x1= class_1
else
cond1= "1=1"

if 选择2
cond2 = x2= class_2
else
cond2= "1=1"

...


Select * from a where cond1 and cond2 and cond3 and cond4 ...



amtyuranus 2003-08-20
  • 打赏
  • 举报
回复
这样写,

sql=select * from a where 1=1;
if(checkbox1->CHECKEN)
SQL=SQL+" AND A.X1=CLASS_1"
IF()

就可以了,不知道你用什么开发的,这和sql 没关系,是技巧问题

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧