我有以下类似于
的查询(继承自遗产)SELECT bla FROM table
WHERE
some.id IN (
SELECT id FROM (
SELECT some FROM tag WHERE bla
UNION
SELECT some FROM dossierinfo WHERE bla
ORDER BY tag LIMIT :limit OFFSET :offset
) AS aggregated
WHERE dossier_type = 'auto'
)
)
完整的SQL位于底部。问题是在PostgreSQL 8.2.x中执行正常。为了测试,我添加了一个嵌入式HSQL 1.8.x db,但随后查询失败并带有
07 Sep 2010 13:55:11.914 [WARN] [main] [org.hibernate.util.JDBCExceptionReporter] - SQL Error: -11, SQLState: 37000
07 Sep 2010 13:55:11.914 [ERROR] [main] [org.hibernate.util.JDBCExceptionReporter] - Unexpected token ORDER, requires ) in statement [...]
所以我发现,HSQL不喜欢内连接中的ORDER。
完整查询:
SELECT *, article_count as articlecount FROM tag
WHERE
tag.id IN (
SELECT id FROM (
SELECT tag.tag AS tag, tag.id as id, tag.article_count as articleCount, 'auto' AS dossier_type FROM tag
WHERE tag.tag_count >= :minimumTagCount AND tag.article_count >= :minimumArticleCount AND
LENGTH(tag.tag) >= :minimumTagLength AND
tag.tag NOT IN (SELECT dossierinfo.name FROM dossierinfo) AND tag.is_stopword = :stopword
UNION
SELECT dossierinfo.name AS tag, dossierinfo.id AS id, dossierinfo.article_count as articleCount,
'manual' AS dossier_type FROM dossierinfo
WHERE dossierinfo.article_count >= :minimumArticleCount
ORDER BY tag LIMIT :limit OFFSET :offset
) AS aggregated
WHERE dossier_type = 'auto'
)
)
PS:我的HSQL设置适用于所有其他测试,因此它正在工作并执行。
答案 0 :(得分:2)
HSQLDB 2.0支持此功能。从http://hsqldb.org/support/下载最新的快照罐,这些罐有错误修复,并且与Postgres和其他方言更兼容。
最新的Hibernate 3.5.5和3.5.6包含一个与快照jar和HSQLDB 1.8.x兼容的方言。
如果您对SQL查询和HSQLDB 2.0有任何问题,您可以报告它并立即修复。
答案 1 :(得分:0)
我的猜测是你需要明确指出ORDER BY的含义:
而不是:
SELECT some FROM tag WHERE bla
UNION
SELECT some FROM dossierinfo WHERE bla
ORDER BY tag LIMIT :limit OFFSET :offset
试试这个:
SELECT some FROM (
SELECT some FROM tag WHERE bla
UNION
SELECT some FROM dossierinfo WHERE bla
) AS union_data
ORDER BY tag LIMIT :limit OFFSET :offset
无论如何我都会这样做,即使在PostgreSQL中也是如此。原始看起来像ORDER BY与UNION中的第二个SELECT一起使用。