I am a 'newbie' to JOOQ.

I have had trouble finding in the tutorial or pdf documentation details on which of several classes to use when using the DSLContext's select statement.

The syntax of the select is not right because, I believe, the where clause contains a reference to the ifd table which is not in the from and not in the join. I am not sure how to accomplish the right syntax.

Any sources of docs would be appreaciated as well as pointers in what I am doing wrong.

Descriptor d = DESCRIPTOR.as("d");
Desclink dl = DESCLINK.as("dl");
Ifdesc ifd = IFDESC.as("ifd");
//
Result<Record2<Integer, Integer>> result = 
      dslContext.select(d.NETWORKID, dl.PARENT)
            .from(d)
            .join(dl).on(dl.PARENT.equal(d.DESCID))
            .where(ifd.DESCID.equal(dl.CHILD))
            .fetch()
      ;
for (Record2 r2 : result) {
    Integer n = r2.getValue( d.NETWORKID);
    Integer p = r2.getValue(dl.PARENT);
    logger.println("nwid : " + n + " p " + p );
}
有帮助吗?

解决方案

I think all you're missing is just another join:

dslContext.select(ifd.IPADDRESS, d.NETWORKID, dl.PARENT)
          .from(d)
          .join(dl).on(dl.PARENT.equal(d.DESCID))
          .join(ifd).on(dl.CHILD.equal(ifd.DESCID)) // another join here
          .fetch();

Note, you can join as many tables as you want to form your own "table expression" to put in the FROM clause in SQL. If you join three tables:

A join B on ... 
  join C on ...

What you're really doing is (pseudo-SQL):

T  := A' join C on ...
A' := A  join B on ...

So, the best way to read the above "triple-join" is:

SELECT ...
FROM ((A join B on ...) join C on ...)

The jOOQ API also supports nesting such JOIN expressions natively, when using Table.join(). For instance:

          ( A.join(B).on(...) ).join(C).on(...)
// A' --> ^^^^^^^^^^^^^^^^^^^^^
// T  --> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The SelectJoinStep.join() method you are using is mere convenience for the above Table.join()

This might be an interesting read for you, explaining 1-2 syntactic things in SQL.

其他提示

I was able to work around the above code without using the explicit join commands.

The code below adds one more returned value, a String.

Result<Record3<String, Integer, Integer>> result      =    
    dslContext.select(ifd.IPADDRESS, d.NETWORKID, dl.PARENT)
            .from(d, dl, ifd)
            .where( 
                 (ifd.DESCID.equal(dl.CHILD))
                .and(dl.PARENT.equal(d.DESCID))
                .and(ifd.IFWASPINGED.isTrue()  ) 
             )
            .fetch()
            ;
for (Record3 r2 : result) {
    Integer n = r2.getValue( d.NETWORKID);
    Integer p = r2.getValue(dl.PARENT);
    String ip = r2.getValue(ifd.IPADDRESS);
    logger.println("nwid : " + n + " p " + p + "  " + ip  );
}

I would still be interested in how this would look using JOINs.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top