我有一个输入屏幕,可以输入8个输入值。输入值如下
tCustomerIDStart - tCustomerIDEnd
tProjectIDStart - tProjectIDEnd
tDateStart - tDateEnd
tProjectStatusStart - tProjectStatusEnd
一旦用户点击我的表单上的提交,我想在我的SQLite数据库上执行查询。我在下面的查询是我想要的基本概要,但是如果用户只输入一对搜索条件,我不太确定如何构造查询。 tDateStart - tDateEnd。我可以获取查询以省略其他子句吗?在调用其他子句之前,WHERE语句中的第一个子句是否始终必须求值为true。如果填写了所有字段,我可以构造一个查询,主要是使用WHERE语法然后使用AND来构建子句的情况。但是,sql是否可以忽略一对空的搜索条件字段?
"SELECT project.start_date, project.projectID, project.project_title, customer.customer, " &\
"project.end_date, project.project_manager, project_status.project_status " &\
"FROM project " &\
"LEFT JOIN customer " &\
"ON project.customerID=customer.customerID " &\
"LEFT JOIN project_status " &\
"ON project.project_statusID=project_status.project_statusID " &\
"WHERE project.projectID BETWEEN " & tProjectIDStart & " AND " & tProjectIDEnd & " " &\
"OR customer.customerID BETWEEN " & tCustomerIDStart & " AND " & tCustomerIDEnd & " " &\
"OR project.project_status_id BETWEEN " & tStatusIDStart & " AND " & tStatusIDEnd & " " &\
"OR project.start_date >= " & tDateStart & " AND " & tDateEnd & " " &\
"ORDER BY date(project.start_date) ASC "
答案 0 :(得分:0)
根据这些值是空还是空来构建查询
如果您开始使用1=1
之类的内容,那么您只需应用其他过滤器并使用AND
启动所有过滤器
sql = "SELECT project.start_date, project.projectID, project.project_title, customer.customer, " &\
"project.end_date, project.project_manager, project_status.project_status " &\
"FROM project " &\
"LEFT JOIN customer " &\
"ON project.customerID=customer.customerID " &\
"LEFT JOIN project_status " &\
"ON project.project_statusID=project_status.project_statusID " &\
"WHERE 1=1 "
if (tProjectIDStart != "" And tProjectIDEnd != "")
{
sql = sql & " AND project.projectID BETWEEN " & tProjectIDStart & " AND " & tProjectIDEnd & " "
}
etc..
sql = sql & "ORDER BY date(project.start_date) ASC "