请教复合查询问题

来源:百度知道 编辑:UC知道 时间:2024/05/09 16:13:41
系统有一张表有 项目部门、项目名称等字段,在页面上有一个查询功能,有项目部门、项目名称两个输入项,如何用SQL语句实现复合查询,当用户只填写了项目部门或者项目名称中一项时,按照一项进行查询;两项都填写了时,按照两项查询。
用一个SQL语句实现,不再页面上进行判断。

sql="select * from Table "
if request("项目部门")<>"" then
sql=sql&" where 项目部门字段名='"&request("项目部门")&"'"
sign=True
end if
if request("项目名称")<>"" and sign=True then
sql=sql&" and 项目名称字段名='"&request("项目名称")&"'"
elseif request("项目名称")<>"" then
sql=sql&" where 项目名称字段名='"&request("项目名称")&"'"

end if
sql=sql&" order by 项目部门字段名 asc"

sql = "select * from Table where 1 = 1";
if (项目部门 != "")
{
sql = sql + "and 项目部门字段名='" + 项目部门 + "'";
}
if (项目名称 != "")
{
sql = sql + "and 项目名称字段名='" + 项目名称 + "'";
}