Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cdlinsen opened this issue Jan 26, 2024 · 0 comments
Closed

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

cdlinsen opened this issue Jan 26, 2024 · 0 comments

Comments

@cdlinsen
Copy link

cdlinsen commented Jan 26, 2024

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

<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的表

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants