Private Sub CmdCx_Click()
Dim strwhere As String
     strwhere = " where 1=1"            ???
     If ChkNo.Value = 1 Then
       strwhere = strwhere & " and 图书编号 like '%" & Trim(TxtNo.Text) & "%'"
    End If
     If ChkAuthor.Value = 1 Then
        strwhere = strwhere & " and 作者 like '%" & Trim(TxtAuthor.Text) & "%'"
         'strwhere = "  作者 like '%" & Trim(TxtAuthor.Text) & "%'"
     End If
end sub这是复选框的一段代码,功能高手一看就知了,就不解释了!
问题是 strwhere = " where 1=1" 这句怎么理解的,为什么用1=1?
先谢了!

解决方案 »

  1.   

    where 1=1 是条件占位符,表示这个条件一直成立,如果不写where1=1 则你就要在下面的sql语句中判断是用where 还是用and 了,如果加了这个占位符,则下面只要有条件,就用 and 条件表示式
      

  2.   

    这是为了和后面的条件做连接
    你也可以初始化成strwhere=""
    然后每次判断如果strwhere=""就是strwhere="……" 否则是strwhere= strwhere & "……"
    这样就麻烦了
      

  3.   

    这是为了后面的语句能够直接用 " And ... " 形式连接。不一定要 " 1=1 " ,任何为真的表达式都可以,例如 :strwhere = " where TRUE"