设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12下一页
返回列表 发新帖
查看: 4129|回复: 11
打印 上一主题 下一主题

[查询] 〔求助〕语句优化〔已解决〕

[复制链接]
跳转到指定楼层
1#
发表于 2010-7-28 16:46:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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
'------------------------------------------------------------------
现在头都搞大了——稀里糊涂了
也不知到这种判断对不对
请求高手优化一下



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2010-7-28 17:19:55 | 只看该作者
if  isnull(Me.厂家)  then
  If   isnull(Me.状态)then
    If   isnull(Me.开始日期)then
      ...
    else
      ...
    end if
  else
    ......
  end if
else
  ...
end if
这样是不是好看一些?
3#
 楼主| 发表于 2010-7-28 19:47:54 | 只看该作者
if  isnull(Me.厂家)  then
  If   isnull(Me.状态)then
    If   isnull(Me.开始日期)then
      . ...
aslxt 发表于 2010-7-28 17:19


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


4#
发表于 2010-7-28 20:30:46 | 只看该作者
本帖最后由 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
5#
 楼主| 发表于 2010-7-28 20:49:58 | 只看该作者
热心的老汉
谢谢额的现状有点糊涂一下还没有反应过来
你的代码还需仔细想想
我正在按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
6#
 楼主| 发表于 2010-7-28 21:05:55 | 只看该作者
'--------------------------------------
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.厂家 & "'"
。。。。。。。。。。。。。。。。。。
其他的还没想仔细
7#
 楼主| 发表于 2010-7-28 21:18:48 | 只看该作者
另弱弱的问一句老汉的SELECT * FROM tbl_表 where True
是否源自select * from table where 1=1
引自网络
'----------------------------------------------------------------------------------------------------------
where 1=1 的总结
  为方便构造动态的多条件之不确定因素的复杂的正确的查询语句所采取的一种“江湖手段”。
  此类方法,一般,在书籍上不常见,而在实际的应用中,人们得从现实角度考虑,即要保证能满足多条件查询、同时还要能应付不确定因素的灵活性,最后还要保证语句不出现任何语法错误。
  该方法不失为一种好方法;但是,却由于这 where 1=1 不知道让多少新手,琢磨了多少次,始终不得要领,同时,还有可能会误导新手误入歧途;
'------------------------------------------------------------
8#
发表于 2010-7-28 22:58:36 | 只看该作者
我的做法和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

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

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

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

10#
发表于 2010-7-29 09:23:30 | 只看该作者
为了加深理解,强烈要求xuwenning 同志重读《切掉尾巴》一文。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-26 01:40 , Processed in 0.108213 second(s), 33 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表