使用 JOOQ 的 SelectQuery 为 JOIN 使用别名
Use alias for JOIN using JOOQ's SelectQuery
我需要使用嵌套 select 编写查询并使用 JOOQ 的 SelectQuery 加入:
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 而不是完整的 table 名称 "public"."child"."parent_id"?
我找到了解决方案。
SelectQuery<ParentRecord> selectQuery = context.selectQuery(subSelectQuery.asTable(PARENT.getName()));
selectQuery.addJoin(CHILD, JoinType.JOIN, CHILD.PARENT_ID.eq(PARENT.as(PARENT.getName()).ID));
我需要使用嵌套 select 编写查询并使用 JOOQ 的 SelectQuery 加入:
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 而不是完整的 table 名称 "public"."child"."parent_id"?
我找到了解决方案。
SelectQuery<ParentRecord> selectQuery = context.selectQuery(subSelectQuery.asTable(PARENT.getName()));
selectQuery.addJoin(CHILD, JoinType.JOIN, CHILD.PARENT_ID.eq(PARENT.as(PARENT.getName()).ID));