Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select constant in JOOQ union

Tags:

java

sql

jooq

I need to do the equivalent of this in JOOQ.

SELECT
  name,
  'companyType' AS resultType
FROM company
UNION ALL
SELECT
  name,
  'userType' AS resultType
FROM "user";

I have figured out how to do unionall in JOOQ fine but I cannot figure out how to select a constant value for results in either select of the union.

like image 744
benstpierre Avatar asked Sep 13 '16 17:09

benstpierre


Video Answer


1 Answers

You're looking for DSL.inline(), which is used for precisely this:

inline("companyType").as("resultType")

Or, the full query:

using(configuration)
  .select(COMPANY.NAME, inline("companyType").as("resultType"))
  .from(COMPANY)
  .unionAll(
   select(USER.NAME, inline("userType").as("resultType"))
  .from(USER))
  .fetch();

Both examples assume you have this static import:

import static org.jooq.impl.DSL.*;

More information can be found here: http://www.jooq.org/doc/latest/manual/sql-building/bind-values/inlined-parameters

like image 136
Lukas Eder Avatar answered Oct 18 '22 14:10

Lukas Eder