有一个表数据库table_a,有字段名称分别为1,2,3,4,5
设置了文本框text1,text2,text3,text3,text4,text5,现在要利用文本框中的输入内容对应数据表中的1-5字段进行查询。
问题是:当text1-text5某一个或几个文本框内容为“”时,则不需要用这一个或几个文本框的内容作为条件查询。(如text2,text4为空时,则使用text1,text3,text5进行查询)
请问这样的语句怎么写?

解决方案 »

  1.   

    table_a,有字段名称分别为a1,a2,a3,a4,a5
    where (a1=text1 or text1="") and (a2=text2 or text2="") ....
      

  2.   

    上面是一种不错的方法,但如果数据量大的话,还是采用构建SQL语句的方式,这样效率要高点
      

  3.   

    zyfhongyang(张韵)没有问题哈,楼主可以试下
      

  4.   

    string strSql = "SELECT * FROM TableName where 1 = 1"
    if(txt1 = string.empty)
    strSql += " and 字段1 = '" + txt1 + "'" ;
    .........
    我更欣赏二楼的方法,很巧妙!
      

  5.   

    where (a1=text1 or text1="") and (a2=text2 or text2="") ....
    请问是不是要将text1改为text1.text?
      

  6.   

    各位大侠能再指点一下吗?多谢了。
    另外如果a1=text1 该成a1 like text1  行吗?
      

  7.   

    AdoRs.Open "select * from table_mtjctj where  (区域= '" &Combo2(0).Text& "' or '" &Combo2(0).Text=""& "') and (媒体名称= '" &Combo2(1).Text& "' or '" &Combo2(1).Text=""& "') and  (发布单位= '" &Combo3(0).Text& "' or '" &Combo3(0).Text=""& "') and (广告性质= '" &Combo3(1).Text& "' or '" &Combo3(1).Text=""& "') and (广告类别= '" &Combo3(2).Text& "' or '" &Combo3(2).Text=""& "') and (广告内容= '" &Combo3(3).Text& "' or '" &Combo3(3).Text=""& "') and (摘要 like '" &Text1(2).Text& "' or '" &Text1(2).Text =""& "')  and (发布日期 between  '" &Text1(0).Text& "'  and '" &Text1(1).Text& "'), Cnn, adOpenKeyset"我是这样写的,但是通不过,请指教。叩谢!!
      

  8.   

    这是VB的是吧,我只回C#
    但你那SQL语句有问题吧,LIKE语句是这样写的吗?如 like '%1%'
      

  9.   

    果然厉害,连用vb都看出来了。like改过了还是不行,到第一个"or"就提示错了。
    苍天啊,大地啊,哪位好心的人救救我
      

  10.   

    where (区域= '" &Combo2(0).Text& "' or '" &Combo2(0).Text=""& "')
    你这个条件不知是什么意思,这里肯定错了
      

  11.   

    帮你改一个:
    where " & iif(trim(combo2(0).text)="",""," 区域='" & trim(combo2(0).text) &"' and ")
      

  12.   

    AdoRs.Open "select * from table_mtjctj where " & IIf(Combo2(0).Text = "", "", " 区域='" & Combo2(0).Text) & "' and &IIf(Combo2(1).Text = "", "", " 媒体名称='" & Combo2(1).Text) & "' and & IIf(Combo3(0).Text = "", "", " 发布单位='" & Combo3(0).Text) & "' and & IIf(Combo3(1).Text = "", "", " 广告性质='" & Combo3(1).Text) & "' and & IIf(Combo3(2).Text = "", "", " 广告类别='" & Combo3(2).Text) & "' and & IIf(Combo3(3).Text = "", "", " 广告内容='" & Combo3(0).Text) & "' and & IIf(text1(2).Text = "", "", " 摘要 like'%" & Combo2(0).Text) & "%' and & IIf(text1(0).Text = "", "", "发布日期 between  ' &Text1(0).Text&  and  &Text1(1).Text)& '", Cnn, adOpenKeysetchuifengde() ,再帮帮我吧,我这样写还是程序通不过啊,帮我查查,谢谢!
      

  13.   

    AdoRs.Open "select * from table_mtjctj 
    where " & IIf(Combo2(0).Text = "", "", " 区域='" & Combo2(0).Text & "'") & " 
    and " & IIf(Combo2(1).Text = "", "", " 媒体名称='" & Combo2(1).Text & "'") & " 
    and " & IIf(Combo3(0).Text = "", "", " 发布单位='" & Combo3(0).Text & "'") & " 
    and " & IIf(Combo3(1).Text = "", "", " 广告性质='" & Combo3(1).Text & "'") & " 
    and " & IIf(Combo3(2).Text = "", "", " 广告类别='" & Combo3(2).Text & "'") & " 
    and " & IIf(Combo3(3).Text = "", "", " 广告内容='" & Combo3(0).Text & "'") & " 
    and " & IIf(text1(2).Text = "", "", " 摘要 like '%" & Combo2(0).Text & "%'") & " 
    and " & IIf(text1(0).Text = "", "", "发布日期 between '" &Text1(0).Text& " and " &Text1(1).Text& "'")
    , Cnn, adOpenKeyset这样就清晰多了……
      

  14.   

    AdoRs.Open "select * from table_mtjctj 
    where " & IIf(Combo2(0).Text = "", "1=1", " 区域='" & Combo2(0).Text & "'") & " 
    and " & IIf(Combo2(1).Text = "", "1=1", " 媒体名称='" & Combo2(1).Text & "'") & " 
    and " & IIf(Combo3(0).Text = "", "1=1", " 发布单位='" & Combo3(0).Text & "'") & " 
    and " & IIf(Combo3(1).Text = "", "1=1", " 广告性质='" & Combo3(1).Text & "'") & " 
    and " & IIf(Combo3(2).Text = "", "1=1", " 广告类别='" & Combo3(2).Text & "'") & " 
    and " & IIf(Combo3(3).Text = "", "1=1", " 广告内容='" & Combo3(0).Text & "'") & " 
    and " & IIf(text1(2).Text = "", "1=1", " 摘要 like '%" & Combo2(0).Text & "%'") & " 
    and " & IIf(text1(0).Text = "", "1=1", "发布日期 between '" &Text1(0).Text& " and " &Text1(1).Text& "'")
    , Cnn, adOpenKeyset更正……and 后不能接空,需加上1=1……
      

  15.   

    " & IIf(text1(0).Text = "", "1=1", "发布日期 between '" &Text1(0).Text& " and " &Text1(1).Text& "'")
    提示" and "  处出错。如何修改?谢谢
      

  16.   

    其实2楼的答案就已经很不错了,基本不用考虑效率问题我也建议建议LZ,如果想用这种字符串拼SQL的方式,最好不要用那么多“+”否则会显得很乱我有个建议ls_sql = "select * from tb1
               where (col1='<text1>' or '<text1>' = '')
                 and (col2='<text2>' or '<text2>' = '') "ls_sql = replace(ls_sql ,"<text1>",text1.text)
    ls_sql = replace(ls_sql ,"<text2>",text2.text)你这样写出来的SQL,看起来就很清晰,而且还能放查询分析器里调试,唯一的缺点就是前台REPLACE慢了一点