日期:2014-05-18  浏览次数:20560 次

sql多条件倒排序问题
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 ")                                                              
if   right(sql,5)= "where "   then   sql=left(sql,clng(len(sql))-5)          


我在条件4加了order   by   id   desc

搜索出来,还是正排序!
很苦恼的问题

例:
if   条件4 <> " "           then   sql=sql& "   and   ziduan4= ' "&条件4 " '   order   by   id   desc   "

------解决方案--------------------
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 ")
if right(sql,5)= "where " then sql=left(sql,clng(len(sql))-5)

'最後加order by id desc

sql= sql & " order by id desc "