selectPage(reqVO, new LambdaQueryWrapperX<VarianceDO>() .eqIfPresent(VarianceDO::getFiscalYear, reqVO.getFiscalYear()) .eqIfPresent(VarianceDO::getWeekEnd, reqVO.getWeekEnd()) .eqIfPresent(VarianceDO::getApprovalStatus, reqVO.getApprovalStatus()) .eq(StrUtil.isNotBlank( reqVO.getOu()),VarianceDO::getOperatingUnit, reqVO.getOu()) .eq(StrUtil.isNotBlank( reqVO.getMu()),VarianceDO::getManagementUnit, reqVO.getMu()) .eq(StrUtil.isNotBlank( reqVO.getSmu()),VarianceDO::getSubManagementUnit, reqVO.getSmu()) .eq(StrUtil.isNotBlank( reqVO.getRankCode()),VarianceDO::getRankCode, reqVO.getRankCode()) .eqIfPresent(VarianceDO::getStaffGpn, reqVO.getEmployeeGPN()) .apply( reqVO.getApprover() != null,"FIND_IN_SET ( '"+ reqVO.getApprover() +"',approver )") .orderByDesc(VarianceDO::getVarianceId)); 我想在这个里面增加一张子表(SELECT va.varianceId FROM ts_variance_fill va WHERE 1=1 and va.charge_back ='N')关联和主表关联主键是varianceId,字表如果传入charge_back 字段为空则只查询主表
时间: 2024-03-09 20:46:18 浏览: 129
你想在这个查询中增加一个子查询来关联主表和子表,并且如果传入的 charge_back 字段为空,则只查询主表。我可以帮你修改这段代码,如下所示:
```
selectPage(reqVO, new LambdaQueryWrapperX<VarianceDO>()
.inSql("varianceId", "SELECT varianceId FROM ts_variance_fill WHERE 1=1 and charge_back ='N'")
.eqIfPresent(VarianceDO::getFiscalYear, reqVO.getFiscalYear())
.eqIfPresent(VarianceDO::getWeekEnd, reqVO.getWeekEnd())
.eqIfPresent(VarianceDO::getApprovalStatus, reqVO.getApprovalStatus())
.eq(StrUtil.isNotBlank(reqVO.getOu()), VarianceDO::getOperatingUnit, reqVO.getOu())
.eq(StrUtil.isNotBlank(reqVO.getMu()), VarianceDO::getManagementUnit, reqVO.getMu())
.eq(StrUtil.isNotBlank(reqVO.getSmu()), VarianceDO::getSubManagementUnit, reqVO.getSmu())
.eq(StrUtil.isNotBlank(reqVO.getRankCode()), VarianceDO::getRankCode, reqVO.getRankCode())
.eqIfPresent(VarianceDO::getStaffGpn, reqVO.getEmployeeGPN())
.apply(reqVO.getApprover() != null, "FIND_IN_SET ( '"+ reqVO.getApprover() +"',approver )")
.orderByDesc(VarianceDO::getVarianceId));
```
在这个查询中,我们使用了 `inSql` 方法来添加一个子查询,该子查询选取了 `ts_variance_fill` 表中的 `varianceId` 字段,当 `charge_back` 字段的值为 `'N'` 时,将其与主表的 `varianceId` 字段进行关联。同时,我们在 `SELECT` 语句中使用了 `1=1` 来保证 SQL 语法的正确性。如果传入的 `charge_back` 字段为空,则只查询主表,因为在这种情况下,子查询中的 `WHERE` 条件将不成立,因此不会返回任何结果。
阅读全文