2 回答
TA贡献1842条经验 获得超21个赞
这就是我会怎么做的:
final StringPath payoutsBbf = stringPath("payoutsBbf");
final String mmbId = "mmb_id";
final String totalPyts = "totalPyts";
sqlQueryFactory.select(users.memberId, stringPath(payoutsBbf, totalPyts))
.from(users).leftJoin(
sqlQueryFactory.select(payout.member_id.as(mmbId), member_id.amount.sum().as(totalPyts))
.from(payout).where(payout.payoutPeriod.lt("2018-01-01")).groupBy(payout.member_id),
payoutsBbf
).on(users.id.eq(stringPath(payoutsBbf, mmbId))).fetch();
TA贡献1790条经验 获得超9个赞
我认为这对你有用。虽然这有点笨拙:
SQLQueryFactory sqlqf; // Should be @Autowired
QUsers qusers = new QUsers();
QPayouts qpayouts = new QPayouts();
Expression<Long> memberId = ExpressionUtils.as(qpayouts.memberId, "mmb_id");
Expression<Double> totalPayouts = ExpressionUtils.as(qpayouts.amount.sum(), "totalPayouts");
SQLQuery<Tuple> payoutsBbf = SQLExpressions.select(memberId, totalPayouts)
.from(qpayouts)
.where(qpayouts.payoutPeriod.lt("2018-01-01")) // Use date object
.groupBy(qpayouts.memberId);
final SimplePath<? extends SQLQuery> payoutsBBfPath = Expressions.path(payoutsBBfPath.getClass(), "payoutsBbf");
List<Tuple> fetch = sqlqf.select(
qusers.memberId,
Expressions.path(payoutsBbf.getClass(), new PathMetadata(payoutsBBfPath, "totalPayouts", PathType.PROPERTY))
)
.from(qusers)
.leftJoin(payoutsBbf, payoutsBBfPath)
.addJoinFlag(" on payoutsBbf.mmb_id = users.id", JoinFlag.Position.BEFORE_CONDITION)
.fetch();
请注意使用定义为 的别名来指定连接列。另请注意使用 在部分中指定子列payoutsBbfExpressions.path()select()
添加回答
举报