Search code examples
sqlmariadbjooq

How can I create a SELECT EXISTS (subquery) with jOOQ?


I want to build and execute a query like this with jOOQ.

SELECT EXISTS( subquery )

For exemple:

SELECT EXISTS(SELECT 1 FROM icona_etiqueta WHERE pvp IS NULL AND unitat_venda = 'GRAMS')

How can I do it? Can it be done?


Solution

  • Found it. I was looking for a selectExists method and got confused by the DSL.exists() predicate constructor.

    There is a much more convenient fetchExists(subquery).

    My specific example is resolved like this:

    create.fetchExists(
            create.selectOne()
                  .from(ICONA_ETIQUETA)
                  .where(ICONA_ETIQUETA.PVP.isNull(),
                         ICONA_ETIQUETA.UNITAT_VENDA.eq('GRAMS'))
        );
    

    Which directly returns a boolean.