注册 登录
编程论坛 ASP技术论坛

关于SQL语句简写问题请教一下

天蝎风 发布于 2010-11-29 12:47, 585 次点击
各位好啊,我想问一下下面的语句该如何简写
<%
if nowxiaoqu="" then
    sql="select * from login order by xiaoqu,id"
  else
    sql="select * from login where xiaoqu="&nowxiaoqu&" order by id"
  end if
  set rs_login=conn.execute(sql)
  do while rs_login.eof=false

if nowbumen="" then
    sql="select * from login order by bumen,id"
  else
    sql="select * from login where bumen="&nowbumen&" order by id"
  end if
  set rs_login=conn.execute(sql)
  do while rs_login.eof=false
  %>
3 回复
#2
jansonwang2010-11-29 14:23
天啊,徒弟,我星期五白给你上课了!
    sql="select * from login where 1=1"
  if nowxiaoqu<>"" then
    sql=sql&"and xiaoqu="&nowxiaoqu
  end if
  if nowbumen<>"" then
    sql=sql&" and bumen="&nowbumen
  end if
    sql=sql&" order by xiaoqu,bumen,id"
  set rs_login=conn.execute(sql)
  do while rs_login.eof=false
  %>
#3
天蝎风2010-11-29 14:40
以下是引用jansonwang在2010-11-29 14:23:07的发言:

天啊,徒弟,我星期五白给你上课了!
    sql="select * from login where 1=1"
  if nowxiaoqu<>"" then
    sql=sql&"and xiaoqu="&nowxiaoqu
  end if
  if nowbumen<>"" then
    sql=sql&" and bumen="&nowbumen
  end if
    sql=sql&" order by xiaoqu,bumen,id"
  set rs_login=conn.execute(sql)
  do while rs_login.eof=false
  %>
哎哟,我真是笨啊~~谢师父指点!
#4
不倒翁h2010-12-01 12:59
原来还可以这么写呀!
1