如何在jOOQ中的同一个表上编写LEFT OUTER JOIN?
如何使用jOOQ编写以下SQL?
SELECT *
FROM food_db_schema.tblCategory AS t1
LEFT OUTER JOIN food_db_schema.tblCategory AS t2 ON t1.category_id = t2.parent_id
WHERE t2.parent_id IS NULL
AND t1.heartbeat = "ALIVE";
数据库是mySQL
how to write following SQL using jOOQ?
SELECT *
FROM food_db_schema.tblCategory AS t1
LEFT OUTER JOIN food_db_schema.tblCategory AS t2 ON t1.category_id = t2.parent_id
WHERE t2.parent_id IS NULL
AND t1.heartbeat = "ALIVE";
database is mySQL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
flesk 的回答很好地描述了如何使用 jOOQ 1.x 来完成此操作。使用别名的自连接或多或少相当于使用别名的常规连接,如手册中所述:
https://www.jooq.org/doc/latest/manual/sql-building/table-expressions/aliased-tables/
在即将发布的版本中2.0 中,别名将变得不那么冗长并且更加类型安全。因此,flesk 的解决方案可以这样简化:
我还在这里描述了一个更复杂的自连接示例:
http://blog.jooq.org/jooq-meta-a-hard-core-sql-proof-of-concept/
flesk's answer depicts nicely how this can be done with jOOQ 1.x. A self-join using aliasing is more or less equivalent to a regular join using aliasing as described in the manual:
https://www.jooq.org/doc/latest/manual/sql-building/table-expressions/aliased-tables/
In the upcoming version 2.0, aliasing will be made less verbose and more type-safe. Hence flesk's solution could be simplified as such:
I have also described a more complex example for a self-join here:
http://blog.jooq.org/jooq-meta-a-hard-core-sql-proof-of-concept/
也许吧
,但是你确定
t2.parent_id
既应该是NULL又等于t1.category_id
吗?编辑:
然后
取决于生成的类、属性和元模型对象的名称。
Maybe
, but are you sure
t2.parent_id
is both supposed to be NULL and equal tot1.category_id
?EDIT:
Then something like
depending on what the generated classes, properties and meta-model objects are called.