Chinaunix首页 | 论坛 | 博客
  • 博客访问: 11880816
  • 博文数量: 187
  • 博客积分: 7517
  • 博客等级: 少将
  • 技术积分: 1981
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-20 18:51
文章分类

全部博文(187)

文章存档

2015年(3)

2013年(4)

2012年(20)

2011年(2)

2010年(96)

2009年(14)

2008年(47)

2007年(1)

我的朋友

分类: 系统运维

2010-05-16 23:38:01

今天在写商城的产品搜索的多条件搜索时研究的,很必须,不然要穷举的话会死人的

ASP中多条件搜索SQL语句:

方法一:

sql="select * from table where 1=1"                                                '避免所有查询条件为空时出错
if 条件1<>""      then sql=sql&" and ziduan1='"&条件1&"'"                  '精确搜索条件1
if 条件2<>""      then sql=sql&" and ziduan2='"&条件2&"'"                   '精确搜索条件2
if 条件3<>""      then sql=sql&" and ziduan3 like '%"&条件3&"%'"        '模糊搜索条件3
if 条件4<>""      then sql=sql&" and ziduan4="&条件4                          '条件4、ziduan4为数值
.
     .
             '以此类推不断增加条件
  .
if 条件n<>""     then sql=sql&" and ziduann='"&条件n&"'"

=======================================================================

方法二:

sql="select * from table where"    
if 条件1<>""      then sql=sql&" and ziduan1='"&条件1&"'"
if 条件2<>""      then sql=sql&" and ziduan2='"&条件2&"'"
if 条件3<>""      then sql=sql&" and ziduan3 like '%"&条件3&"%'"
if 条件4<>""      then sql=sql&" and ziduan4="&条件4

.
.
                               '类推增加条件
.   
if 条件n<>""     then sql=sql&" and ziduann='"&条件n&"'"
sql=replace(sql,"where and","where")
                                '判断当有条件不为空时,转换第一个where and
if right(sql,5)="where" then sql=left(sql,clng(len(sql))-5)     '判断所有条件都为空时,去掉where


注:要分页时,分页程序里可以需要带上的条件可以这样写:

keywords="条件1="&条件1&"&条件2="&条件2&"&条件3="&条件3&"&条件4="&条件4

阅读(1619) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~