Office中国论坛/Access中国论坛

标题: 〔求助〕语句优化〔已解决〕 [打印本页]

作者: xuwenning    时间: 2010-7-28 16:46
标题: 〔求助〕语句优化〔已解决〕
本帖最后由 xuwenning 于 2010-7-29 08:07 编辑

查询有四个条件:
厂家,状态,开始日期,结束日期
'--------------------------------------
strSql1 = "SELECT * FROM tbl_表"
If isnull(Me.厂家)  and  isnull(Me.状态)and  isnull(Me.开始日期) and  isnull(Me.结束日期)then
strSql1 = strSql1 & ";"
end if
If isnull(Me.厂家)  and  isnull(Me.状态)and  isnull(Me.开始日期) and  not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE 日期 =" & Me. 结束日期& ";"
end if
If isnull(Me.厂家)  and  isnull(Me.状态)and not  isnull(Me.开始日期) and  isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE 日期 =" & Me. 开始日期& ";"
end if
If isnull(Me.厂家)  and  not isnull(Me.状态)and  isnull(Me.开始日期) and  isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE Flag=" & Me.状态 & ";"
end if
If not isnull(Me.厂家)  and  isnull(Me.状态)and  isnull(Me.开始日期) and  isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE厂家=" & Me. 厂家& ";"
end if
If isnull(Me.厂家)  and  isnull(Me.状态)and not  isnull(Me.开始日期) and  not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期  & "# And  #"&  Me. 结束日期  &"#)"
end if
If isnull(Me.厂家)  and  isnull(Me.状态)and not  isnull(Me.开始日期) and  not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期  & "# And  #"&  Me. 结束日期  &"#)"
end if
If isnull(Me.厂家)  and  not  isnull(Me.状态)and  not  isnull(Me.开始日期) and  not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期  & "# And  #"&  Me. 结束日期  &"# Flag=" & Me.状态 & ";"
end if
If not  isnull(Me.厂家)  and  not  isnull(Me.状态)and  not  isnull(Me.开始日期) and  not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期  & "# And  #"&  Me. 结束日期  &"# )and Flag=" & Me.状态 & " and 厂家=" & Me. 厂家& ";"
end if
if not isnull(Me.厂家) and not isnull(Me.状态)and isnull(Me.开始日期)and isnull(Me.结束日期)then
strSql1 & " WHERE Flag=” & Me.状态 & “and 厂家 =" & Me.厂家 & ";"
end if
'------------------------------------------------------------------
现在头都搞大了——稀里糊涂了
也不知到这种判断对不对
请求高手优化一下




作者: aslxt    时间: 2010-7-28 17:19
if  isnull(Me.厂家)  then
  If   isnull(Me.状态)then
    If   isnull(Me.开始日期)then
      ...
    else
      ...
    end if
  else
    ......
  end if
else
  ...
end if
这样是不是好看一些?
作者: xuwenning    时间: 2010-7-28 19:47
if  isnull(Me.厂家)  then
  If   isnull(Me.状态)then
    If   isnull(Me.开始日期)then
      . ...
aslxt 发表于 2010-7-28 17:19


if语句只能嵌套7层
4个条件的组合好像已经超过7层了



作者: todaynew    时间: 2010-7-28 20:30
本帖最后由 todaynew 于 2010-7-28 20:37 编辑
查询有四个条件:
厂家,状态,开始日期,结束日期
'--------------------------------------
strSql1 = ...
xuwenning 发表于 2010-7-28 16:46


简单问题何必复杂化呢?
dim ssql as string
dim swh as string
ssql = "SELECT * FROM tbl_表 where True "
swh=""
If isdate(Me.结束日期)=true then swh=swh & " and 日期 <=#" & Me. 结束日期 & "#"
If isdate(Me.开始日期)=true then swh=swh & " and 日期 >=#" & Me. 开始日期 & "#"
If isnull(Me.状态)=false then swh=swh & " and Flag=" & Me.状态
If isnull(Me.厂家)=false then swh=swh & " and 厂家='" & Me.厂家 & "'"
ssql=ssql & swh
作者: xuwenning    时间: 2010-7-28 20:49
热心的老汉
谢谢额的现状有点糊涂一下还没有反应过来
你的代码还需仔细想想
我正在按aslxt 的方法改呢
'--------------------------------------
strSql1 = "SELECT * FROM tbl_表"

if  isnull(Me.厂家)  then
  If   isnull(Me.状态)then
    If   isnull(Me.开始日期)then
       If   isnull(Me.结束日期)then
        strSql1 = strSql1 & ";"
    else
              strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期  & "# And  #"&  Me. 结束日期  &"# )and Flag=" & Me.状态 & " and 厂家=" & Me. 厂家& ";"
    end if
   
  else
    ......
  end if
else
  ...
end if
作者: xuwenning    时间: 2010-7-28 21:05
'--------------------------------------
dim ssql as string
dim swh as string
ssql = "SELECT * FROM tbl_表 where True "
swh=""
If isdate(Me.结束日期)=true then swh=swh & " and 日期 <=#" & Me. 结束日期 & "#"
If isdate(Me.开始日期)=true then swh=swh & " and 日期 >=#" & Me. 开始日期 & "#"
If isnull(Me.状态)=false then swh=swh & " and Flag=" & Me.状态
If isnull(Me.厂家)=false then swh=swh & " and 厂家='" & Me.厂家 & "'"
ssql=ssql & swh
'--------------------------------------
对老汉的代码自己的理解一下不知对不对
If isdate(Me.结束日期)是true        ’结束日期不为空
则swh=" and 日期 <=#" & Me. 结束日期 & "#"
继续..............
If isdate(Me.开始日期)是true         ’开始日期不为空
则swh=" and 日期 <=#" & Me. 结束日期 & "# and 日期 >=#" & Me. 开始日期 & "#"
继续...............
If isnull(Me.状态)=false是true         ’状态不为空
则swh=" and 日期 <=#" & Me. 结束日期 & "# and 日期 >=#" & Me. 开始日期 & "# and Flag=" & Me.状态
继续...............
If isnull(Me.厂家)=false是true’厂家不为空
则swh=" and 日期 <=#" & Me. 结束日期 & "# and 日期 >=#" & Me. 开始日期 & "# and Flag=" & Me.状态 and 厂家='" & Me.厂家 & "'"
。。。。。。。。。。。。。。。。。。
其他的还没想仔细

作者: xuwenning    时间: 2010-7-28 21:18
另弱弱的问一句老汉的SELECT * FROM tbl_表 where True
是否源自select * from table where 1=1
引自网络
'----------------------------------------------------------------------------------------------------------
where 1=1 的总结
  为方便构造动态的多条件之不确定因素的复杂的正确的查询语句所采取的一种“江湖手段”。
  此类方法,一般,在书籍上不常见,而在实际的应用中,人们得从现实角度考虑,即要保证能满足多条件查询、同时还要能应付不确定因素的灵活性,最后还要保证语句不出现任何语法错误。
  该方法不失为一种好方法;但是,却由于这 where 1=1 不知道让多少新手,琢磨了多少次,始终不得要领,同时,还有可能会误导新手误入歧途;
'------------------------------------------------------------

作者: zyp    时间: 2010-7-28 22:58
我的做法和todaynew 版主比较接近

dim strSql1 as string
Dim TjCj as string,TjZt as string,TjRqS as tring,TjRqE as string
if Me.厂家<>"" and isnull(Me.厂家)=false then Tjcj=" AND  厂家='" & trim(Me.厂家) & "'"
if Me.状态<>"" and isnull(Me.状态)=false then Tjzt=" AND Flag="& Me.状态
if Me.开始日期<>"" and isnull(Me.开始日期)=false then TjRqS=" AND 日期>=#"&  Me.开始日期 &"#"
if Me.开始日期<>"" and isnull(Me.结束日期)=false then TjRqS=" AND 日期<=#"&  Me.结束日期 &"#"
strSql1="SELECT * FROM tbl_表 WHERE 1=1 "& TjCj & TjZt & TjRqS  & TjRqE

至于两个日期输入框就不要去判断了,直接将它的格式设为日期格式就可以了,这样只能输入日期而不能输入其它格式的数据了
作者: xuwenning    时间: 2010-7-29 08:00
我的做法和todaynew 版主比较接近

dim strSql1 as string
Dim TjCj as string,TjZt as string,TjRqS as ...
zyp 发表于 2010-7-28 22:58

zyp 的代码容易理解
昨天脑袋进水了稀里糊涂的
没反应过来,把简单的事情搞复杂了
再次感谢各位网友的支持了
谢谢


作者: todaynew    时间: 2010-7-29 09:23
为了加深理解,强烈要求xuwenning 同志重读《切掉尾巴》一文。
作者: todaynew    时间: 2010-7-29 13:08
1=1太山寨了吧?true的可读性要强一些,也便于理解。

作者: xuwenning    时间: 2010-7-29 15:01
为了加深理解,强烈要求xuwenning 同志重读一文。
todaynew 发表于 2010-7-29 09:23

收到
一定仔细理解








欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3