使用JOOQ的SelectQuery为JOIN使用别名

时间:2015-03-04 14:18:49

标签: sql join alias jooq

我需要使用JOOQ的SelectQuery编写带嵌套select和join的查询:

    SELECT *
    FROM (select * from 
    "public"."parent" 
    order by "public"."parent"."setup_time" 
    desc limit 10 offset 0) as "parent1" 
    join "public"."child" 
    on "public"."child"."parent_id" = "parent1"."id" 
    where "public"."child"."name" = 'test'

所以,我写过这样的话:

SelectQuery<ParentRecord> subSelectQuery = context.selectQuery(PARENT);
selectQuery.addJoinOnKey(CHILD, JoinType.JOIN, CHILD.PARENT_ID);

但它会生成类似

的sql代码
join "public"."child" on "public"."child"."parent_id" =  "public"."parent"."id

如何使用别名 parent1 而不是完整的表名&#34; public&#34;。&#34; child&#34;。&#34; parent_id&#34;

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。

SelectQuery<ParentRecord> selectQuery = context.selectQuery(subSelectQuery.asTable(PARENT.getName()));
selectQuery.addJoin(CHILD, JoinType.JOIN, CHILD.PARENT_ID.eq(PARENT.as(PARENT.getName()).ID));