Specifying the alias name for a JOIN

81 views
Skip to first unread message

Murray Cumming

unread,
May 14, 2012, 3:50:49 AM5/14/12
to jooq...@googlegroups.com
When defining a JOIN in my SELECT query, how can I specify its alias
name. I need this to resolve ambiguity later in the query when I have
two joins to the same table.

Now I'm doing this:
step.leftOuterJoin(tableName + " AS " + aliasName).on(condition);

But I would like to use a Table<Record> instead of that table name
string, via the new Factory.tableByName() method.

--
Murray Cumming
mur...@murrayc.com
www.murrayc.com
www.openismus.com

Lukas Eder

unread,
May 14, 2012, 3:53:57 AM5/14/12
to jooq...@googlegroups.com
Hello Murray,

> Now I'm doing this:
>  step.leftOuterJoin(tableName + " AS " + aliasName).on(condition);

Yes, that is the simplest way

> But I would like to use a Table<Record> instead of that table name
> string, via the new Factory.tableByName() method.

You can use as() on any org.jooq.Table. Write:
tableByName(tableName).as(aliasName)

Murray Cumming

unread,
May 14, 2012, 3:59:53 AM5/14/12
to jooq...@googlegroups.com
So would this have the intended effect?

step.leftOuterJoin(Factory.tableByName("sometable").as("somealias")).on(condition);
?

Lukas Eder

unread,
May 14, 2012, 4:07:37 AM5/14/12
to jooq...@googlegroups.com
> So would this have the intended effect?
>
> step.leftOuterJoin(Factory.tableByName("sometable").as("somealias")).on(condition);

Yes this would work. An example:

create.select()
.from(tableByName("PRODUCTS").as("p"))
.where(fieldByName("p", "NUMBER").equal("1234"))
.fetch();

Cheers
Lukas
Reply all
Reply to author
Forward
0 new messages