求一sql语句

xq02 2004-10-11 02:33:59
我想实现一个查询,既可以联合查询也可以单独查询,表如下
table
n_id n_name n_date n_bem
01 aa 2004-10-10 aaaa
02 bb 2004-10-11 bbbb
03 cc 2004-10-12 cccc

举个例子来说
select * from table where "+myform.RtCombText1+" and "+myform.RtCombText2+"
其中 myform.RtCombText1 返回的值有时是 n_id=值1,有时返回的值是空,
myform.RtCombText2 返回的值有时是 n_name=值2,有时返回的是空,
如果两个都不返回空的话,那就应该是一个这样的查询了:select * from table where n_id=值1 and n_name=值2,但是如果其中一个返回一个空值的话,中间那个and怎么处理呢?不知这样说各位能否看明白,希望能帮写一个sql语句.
...全文
91 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xq02 2004-10-11
  • 打赏
  • 举报
回复
为什么iif函数在查询分析器中都不能执行?
victorycyz 2004-10-11
  • 打赏
  • 举报
回复
dim strSQL as string,strWhere as string

strSQL=" select * from table "

if myform.rtcombtext1<>"" then strWhere=" and " & myform.rtcombtext1
if myform.rtcombtext1<>"" then strWhere=strWhere & " and " & myform.rtcombtext2

if strWhere<>"" then strSQL=strSQL & "where " & mid(strWhere,6)

rs.open strSQL,cn
zjcxc 元老 2004-10-11
  • 打赏
  • 举报
回复
"select * from table where 1=1"+iif(myform.RtCombText1="",""," and n_id='"+myform.RtCombText1+"'")+iif(myform.RtCombText2="",""," and n_name='"+myform.RtCombText2+"'")

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧