文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>asp中多条件搜索的sql语句算法艺术

asp中多条件搜索的sql语句算法艺术

时间:2010-05-16  来源:mx11

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

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

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载