QueryDSL 按顺序合并
QueryDSL coalesce in order by
我正在尝试连接两个表并输出它们并按 order by coalesce(tableA.name, tableB.name)
(不是 order by tableA.name, tableB.name
)等两个字段的字母顺序对它们进行排序,因此结果应该类似于:
tableA.name tableB.name
A null
B null
null C
D null
null E
在普通的 SQL 中它工作正常但是当我尝试使用 QueryDSL 时它会向生成的 select 语句添加额外的列并仅按第一个指定的列排序:
//java code
query.orderBy(qTableA.name.coalesce(qTableB.name).asc());
//generated sql code
SELECT ...
COALESCE(tablea_.NAME, tableb_.NAME) AS col_9_0_
FROM ...
WHERE ...
ORDER BY tablea1_.NAME ASC
谁能告诉我为什么会这样,是否有可能让它像我期望的那样工作?
试试这个:
final Coalesce<String> coalesce =
new Coalesce<>(String.class).add(optionalA).add(optionalB);
在您的 select 字段中使用合并,因此在您的 order by 子句中:
.orderBy(coalesce.asc()) // or desc()
我正在尝试连接两个表并输出它们并按 order by coalesce(tableA.name, tableB.name)
(不是 order by tableA.name, tableB.name
)等两个字段的字母顺序对它们进行排序,因此结果应该类似于:
tableA.name tableB.name
A null
B null
null C
D null
null E
在普通的 SQL 中它工作正常但是当我尝试使用 QueryDSL 时它会向生成的 select 语句添加额外的列并仅按第一个指定的列排序:
//java code
query.orderBy(qTableA.name.coalesce(qTableB.name).asc());
//generated sql code
SELECT ...
COALESCE(tablea_.NAME, tableb_.NAME) AS col_9_0_
FROM ...
WHERE ...
ORDER BY tablea1_.NAME ASC
谁能告诉我为什么会这样,是否有可能让它像我期望的那样工作?
试试这个:
final Coalesce<String> coalesce =
new Coalesce<>(String.class).add(optionalA).add(optionalB);
在您的 select 字段中使用合并,因此在您的 order by 子句中:
.orderBy(coalesce.asc()) // or desc()