We often encounter multiple query problems, and long SQL statements are often confused. In particular, when the client part of the query conditions, such as the ordinary method will be even more difficult. The following ingeniously uses the identity of the "where 1=1" (in fact many, let it be true) to solve the problem.
' Subject Information title
' Company name to release information
' Content to post information
' Address Company
' Infomation Company profile
' Note related
The above values are submitted by Form, and then the corresponding values are obtained by: Subject=trim (Request.Form ("subject")).
<%
' This function is critical! ---------------------------
Function SQL (A,B,SQLS)
If b<> "then" if the client does not submit this value, the corresponding SQL statement is not generated.
Sqls=sqls & "and" & A & "Like '%" & B & "%"
End If
Sql=sqls
End Function
'-----------------Call the database
Set conn=server.createobject ("ADODB. Connection ")
Dbpath=server.mappath ("/database/mydb.mdb")
Conn.Open "Driver={microsoft Access driver (*.mdb)};p wd=;d bq=" & DBPath
Set rs=server.createobject ("ADODB. Recordset ")
Sqls= "SELECT * FROM MyTable where 1=1"
' The following calls the above function, which can be called very many (theoretically arbitrary)
Sqls=sql ("Subject", Subject,sqls)
Sqls=sql ("Company", Company,sqls)
Sqls=sql ("Content", Content,sqls)
Sqls=sql ("Address", ADDRESS,SQLS)
Sqls=sql ("infomation", Infomation,sqls)
Sqls=sql ("note", Note,sqls)
Sqls=sqls & "ORDER BY id DESC"
Rs.Open sqls,conn,3,2
%>
Conclusion
If there are no key function functions of SQL (A,B,SQLS), we can imagine how much it takes to judge one sentence after another!