未找到 HSQLDB HAVING 对象
Posted
技术标签:
【中文标题】未找到 HSQLDB HAVING 对象【英文标题】:HSQLDB HAVING object not found 【发布时间】:2016-03-08 18:36:44 【问题描述】:您好,我无法在 hsqldb 中使用 mysql 查询来进行单元测试。
抱歉,我无法向您展示真正的代码。 我试图让它尽可能简单:
SELECT A.id,
(SELECT count(*) FROM SomeOtherTable) as myAlias
FROM Orders A WHERE A.someKz = 2 HAVING 1 = myAlias;
会导致
用户缺少权限或找不到对象:MYALIAS
所以我的问题是,如何在HAVING
子句中访问变量myAlias
?
此查询在您看来可能并不完整,但它非常简化。例如,有几个带有变量的复杂子选择。
更新
这个问题基本一样
SELECT A.id, (SELECT count(*) as anzahl1 FROM ABTable p WHERE p.Aid = A.id AND p.refNR > 0) as anz1, (SELECT count(*) as anzahl1 FROM ABTable p WHERE p.Aid = A.id ) as anz2, (SELECT count(*) as anzahl1 FROM ABTable p WHERE p.Aid = A.id AND p.SID = 18 ) as anz3 FROM ABable A WHERE
A.someInt IN(1,2)
AND A.someString > '20150308190127'
AND(SELECT Count(*) FROM DTable D WHERE D.Aid = A.id ) = 0 HAVING anz1 = anz2 AND anz3 < anz2 ORDER BY someString ASC LIMIT 1;
【问题讨论】:
不允许有重复的别名。 docs.oracle.com/cd/E12825_01/epm.111/esb_dbag/… @SkyWalker 重复的别名应该在哪里? 【参考方案1】:对于 HSQLDB,您的示例可以写成这样。仅当 Orders 表包含单行时,它才会返回结果。但是如果1
实际上是SELECT列表中的另一个表达式,则需要将其写出来:
SELECT A.id, 1 as myAlias
FROM Orders A WHERE A.someKz = 2 AND (SELECT count(*) FROM Orders) = 1
SELECT A.id, A.acol * 12 as myAlias
FROM Orders A WHERE A.someKz = 2 AND (SELECT count(*) FROM Orders) = A.acol *12
您的完整查询要复杂得多。因此,您应该使用包装 SELECT 语句编写它:
SELECT * FROM (
SELECT A.id, (SELECT count(*) as anzahl1 FROM ABTable p WHERE p.Aid = A.id AND p.refNR > 0) as anz1, (SELECT count(*) as anzahl1 FROM ABTable p WHERE p.Aid = A.id ) as anz2, (SELECT count(*) as anzahl1 FROM ABTable p WHERE p.Aid = A.id AND p.SID = 18 ) as anz3 FROM ABable A
WHERE A.someInt IN(1,2)
AND A.someString > '20150308190127'
AND(SELECT Count(*) FROM DTable D WHERE D.Aid = A.id ) = 0
) WHERE anz1 = anz2 AND anz3 < anz2 ORDER BY someString ASC LIMIT 1;
【讨论】:
你能用第二个例子解释一下吗? 第二个例子展示了如何使用表达式来代替常量1
。您在评论中说1
实际上是另一个别名。以上是关于未找到 HSQLDB HAVING 对象的主要内容,如果未能解决你的问题,请参考以下文章