[英]How can I select a column by name in jooq, when result record has two columns of different tables with the same name?
[英]jooq getValue(String fieldName) Columns with the same name in different tables
SQL:
SELECT tblA.hostname, tblB.hostname FROM tblA, tblB ...
嘗試從每個表獲取主機名時,它不起作用。 例如,
String clientHostname = (String) result.getValue("tblA.hostname");
String serverHostname = (String) result.getValue("tblB.hostname");
執行異常:[IllegalArgumentException:行(主機名,主機名)中不包含字段(tblA.hostname)]
然后,嘗試通過以下方法更正此問題:
String clientHostname = (String) result.getValue("hostname");
String serverHostname = (String) result.getValue("hostname");
在clientHostname = tblA.hostname和serverHostname = tblB.hostname的情況下,這不會返回所需的行為,而只會為兩者返回tblA.hostname。
如何獲得兩列的值?
你有嘗試過嗎?
“從tblA,tblB中選擇tblA.hostname AS tblAhostname AS ,tblB.hostname AS tblBhostname ...”
String clientHostname = (String) result.getValue("tblAhostname"); String serverHostname = (String) result.getValue("tblBhostname");
除了按照Erik的建議顯式重命名列之外,您還可以訪問合格的列名,如下所示:
String clientHostname = result.getValue(field(name("tblA", "hostname"), String.class));
String serverHostname = result.getValue(field(name("tblB", "hostname"), String.class));
該解決方案利用:
DSL.field(Name, Class)
構造一個“普通SQL”字段 DSL.name(String...)
構造一個標識符 可以在手冊中找到有關此API的更多信息:
請注意,還有一個待處理的功能請求#4578允許您按預期的方式使用API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.