The most concise multi-query Solution
[Preface]
We often encounter multiple query problems, and long SQL statements are often confusing. Especially when the client partially fills in the query conditions, it is even more difficult to use common methods.
The following cleverly uses the "where 1 = 1" equation (in fact many, let it be set to true) to solve this problem.
[Body Summary]
'Subject information title
Name of the company whose company information is published
'Content content
'Address company address
'Infomation Company Profile
'Note
The preceding values are submitted by form and then obtained through: Subject = trim (request. Form ("subject.
<%
'The function is critical! ---------------------------
Function SQL (a, B, sqls)
If B <> "" then', if the client does not submit this value, no corresponding SQL statement is 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)}; Pwd =; DBQ =" & dbpath
Set rs = server. Createobject ("ADODB. recordset ")
Sqls = "select * From mytable where 1 = 1"
'Call the above function below, and you can call multiple (theoretically any)
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 is no key function SQL (a, B, sqls), we can imagine the number of statements that need to be judged one by one!