我正在 MySQL 数据库上使用 iReport Designer 和 JasperServer 5.6,我试图让我的报表在参数为 null 时返回所有结果。
我一直在查看此处的文档:http://jasperreports.sourceforge.net/sample.reference/query/ ,这非常有帮助,只是它没有我想要的东西。我得到的最接近的信息在文档中:
The $X{EQUAL, column_name, parameter_name} clause function
The function expects three mandatory clause tokens:
- The first token represents the function ID and always takes the fixed value EQUAL.
- The second token is the SQL column (or column combination) to be used in the clause.
- The third token is the name of the report parameter that contains the value to compare to.
If the parameter's value is not null, the function constructs a = ? clause. If the parameter's value is null, the function generates a IS NULL clause.
我输入的所有参数都是我想要查看的记录的 id,所以当我使用它时,我不会得到任何结果,因为 id 或主键不能为空。
例如。
SELECT *
FROM User
WHERE $X{EQUAL, user.id, user_id}
输入 1 将返回用户 id 1,不输入任何内容或 null,将不会返回任何内容。我希望它返回的是表中的所有用户。
是否有一个简单的解决方案可以解决此问题,例如在发生这种情况时让此函数返回 IS NOT NULL? JasperServer 或 iReports 中是否还有其他内容可以帮助我,或者当我将此参数设置为 null 时,我可以在 SQL 中执行哪些操作来忽略 WHERE 子句?
最佳答案
您可以在查询的 WHERE 子句中使用以下逻辑来实现您的要求(这适用于 postgres,但也应该适用于 MySQL):
where (($P{parameter1} is null) or (user.id = $P{parameter1}))
如果参数为 null,OR 比较器的第一部分将返回 true,查询将忽略表达式的其余部分,这应该为您提供用户表中的所有用户。如果不为空,它将跳过第一部分,并执行传递给参数的 user.id 的查询。
希望这有帮助!
关于mysql - 如何将 JasperReports 5.6 Equal Clause 函数设置为 IS NOT NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27004786/