简体   繁体   中英

H2 and COALESCE with sub-select column, not found

I have a query that works in MySQL but also needs to function with H2. The query performs a sub-select to generate a column that is used for ordering. The nulls are replaced with a value so they are set last in the order. H2 complains about bad grammer that the sub-query column does not exist, it works great with MYSQL. I have MYSQL mode set on the H2.

This is the query:

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

If the query needs to work with two systems, I would suggest that you use a subquery:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM