读书人

sql多条件倒排序有关问题

发布时间: 2012-02-12 17:16:34 作者: rapoo

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 "

读书人网 >SQL Server

热点推荐