H2和COALESCE带有子选择列,未找到

时间:2017-11-20 16:15:03

标签: mysql sql h2 coalesce

我有一个在MySQL中运行的查询,但也需要在H2中运行。 查询执行子选择以生成用于排序的列。空值将替换为值,因此它们在顺序中设置为最后一个。 H2抱怨语法不好,子查询列不存在,它适用于MYSQL。我在H2上设置了MYSQL模式。

这是查询:

SELECT
    OO.ID,
    OO.UUID,
    OO.BLOCKED,
    OO.CREATED,
    OO.UPDATED,
    (SELECT
        VALUE
    FROM PUBLIC.OOSER_PA
    WHERE (ATT_ID = (SELECT
        ID
    FROM PUBLIC.PAT
    WHERE NAME = 'ooserFirst'))
        AND (OOSER_ID = OO.ID)) AS SORTED
FROM PUBLIC.OOSER OO
ORDER BY =COALESCE(SORTED, 'zzzzzzzzzz')
LIMIT 0 OFFSET 10

1 个答案:

答案 0 :(得分:2)

如果查询需要使用两个系统,我建议您使用子查询:

SELECT oo.*
FROM (SELECT OO.ID, OO.UUID, OO.BLOCKED, OO.CREATED, OO.UPDATED,
             (SELECT op.VALUE
              FROM PUBLIC.OOSER_PA op
              WHERE op.ATT_ID = (SELECT p.ID
                                 FROM PUBLIC.PAT p
                                 WHERE p.NAME = 'ooserFirst'
                                ) AND
                    op.OOSER_ID = OO.ID
             ) AS SORTED
      FROM PUBLIC.OOSER OO
     ) oo
ORDER BY COALESCE(SORTED, 'zzzzzzzzzz')
LIMIT 0 OFFSET 10;