yulichang / mybatis-plus-join

支持连表查询的mybatis-plus,mybatis-plus风格的连表操作提供wrapper.leftJoin(),wrapper.rightJoin()等操作
https://yulichang.github.io/mybatis-plus-join-doc/
Apache License 2.0
971 stars 113 forks source link

Union all 分页查询生成的语句不正确 #108

Open cdlinsen opened 3 months ago

cdlinsen commented 3 months ago

当前使用版本(必填,否则不予处理)

<mybatis-plus-join.version>1.4.8.1</mybatis-plus-join.version>

该问题是如何引起的?(确定最新版也有问题再提!!!)

Union all分页查询生成的语句不正确,SqlServer数据库

重现步骤(如果有就写完整)

Java代码如下:

MPJLambdaWrapper<M0Org> a=new MPJLambdaWrapper<M0Org>(M0Org.class)
        .eq(!StringUtil.isNullOrEmpty(dataGroupOrgId), M0Dept::getDataGroupOrgID, dataGroupOrgId)
        .selectAs(M0Org::getOrgID, A1Org::getOrgId)
        .unionAll(M0Dept.class,u->u
                .eq(!StringUtil.isNullOrEmpty(dataGroupOrgId), M0Dept::getDataGroupOrgID, dataGroupOrgId)
                .selectAs(M0Org::getOrgID, A1Org::getOrgId)
        );
Page<A1Org> pageData = a.page(new Page<A1Org>(current, pageSize), A1Org.class);

生成的Sql语句如下:

WITH selectTemp AS (
    SELECT 
        TOP 100 
        PERCENT ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __row_number__, 
        t.OrgID AS orgId 
    FROM 
        M0Org t 
    UNION ALL 
    SELECT 
        t.OrgID AS orgId 
    FROM 
        M0Dept t
) 
SELECT * FROM selectTemp WHERE __row_number__ BETWEEN 1 AND 10000 ORDER BY __row_number__

Union的第一个表为什么会生成Top 100,然后row_number生成的为位置也不对

Union之后获取Count,生成的语句也不对

Java代码如下:

        MPJLambdaWrapper<M0Org> mpjLambdaWrapper = new MPJLambdaWrapper<M0Org>(M0Org.class)
                .eq(!StringUtil.isNullOrEmpty(dataGroupOrgId), M0Org::getDataGroupOrgID, dataGroupOrgId)
                .unionAll(M0Dept.class, union -> union
                        .eq(!StringUtil.isNullOrEmpty(dataGroupOrgId), M0Dept::getDataGroupOrgID, dataGroupOrgId)
                );
        long count=mpjLambdaWrapper.count();

生成的Sql语句如下:

SELECT COUNT( * ) FROM M0Org t

获取count(*)的时候,生成的语句没有union all的表