我的查询如下:
select column_date, field1, field2, sum(field3) from table1
where field1 like '*xyz' and
column_date between [please enter start date] and [please enter end date]
group by column_date, field1, field2
当我将两个参数框都留空时,输出为空白。但是我希望它们在输出中如下所示
这是我显示输出的asp代码。它在我在两个文本框中插入值时都有效,但是如果我将它们中的任何一个或两者都留空,则会显示错误。
<html>
<body>
<%
dim startdate, enddate
startdate = Request.Form ("startdate")
enddate = Request.Form("enddate")
set conn = Server.CreateObject ("ADODB.Connection")
conn.open "connectionname"
set rs = Server.CreateObject ("ADODB.Recordset")
Sqlquery = "queryname '" & startdate & "', '" & enddate &'" "
rs.open sql, conn %>
<table>
<tr>
<%
For each x in rs.fields
response.write ("<th>" & x.name & "</th>")
next %> </tr>
<tr><% Do Until rs.EOF %>
<% For each x in rs.Fields %>
<td>Response.write (x.value)</td>
<%next
rs.movenext %>
</tr>
<% loop
rs.close
conn.close %>
</table>
</body>
</html>
答案 0 :(得分:1)
我将从一个简单的SELECT查询开始,以找出WHERE子句。一旦正确工作,将其转换为GROUP BY查询。
那么看看这个是否针对正确的记录:
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
SELECT t.field1, t.field2, t.field3, t.column_date
FROM table1 AS t
WHERE
t.field1 Like '*xyz'
AND (
(t.column_date Between [Start Date] And [End Date])
OR ([Start Date] Is Null And t.column_date = [End Date])
OR (t.column_date = [Start Date] And [End Date] Is Null)
OR ([Start Date] Is Null And [End Date] Is Null)
);
假设第一个查询返回正确的行,我认为这个GROUP BY查询可能会给你你想要的东西。
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
SELECT t.column_date, t.field1, t.field2, sum(t.field3)
FROM table1 AS t
WHERE
t.field1 Like '*xyz'
AND (
(t.column_date Between [Start Date] And [End Date])
OR ([Start Date] Is Null And t.column_date = [End Date])
OR (t.column_date = [Start Date] And [End Date] Is Null)
OR ([Start Date] Is Null And [End Date] Is Null)
)
GROUP BY t.column_date, t.field1, t.field2;
如果从经典ASP运行此查询,则需要将ANSI通配符%
替换为Access样式的*
通配符。
t.field1 Like '%xyz'
或者考虑使用ALike
和ANSI通配符而不是Like
。这样,查询将始终运行相同,而无需切换通配符。
t.field1 ALike '%xyz'
此外,使用经典ASP,从ADO Command对象运行查询,并为参数提供值。