首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql多条件倒排序有关问题

2012-02-12 
sql多条件倒排序问题sql select*fromtablewhere if条件1 thensqlsql& andziduan1&条件1& if

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 "

热点排行