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

3.2.5 查询后自动添加了orderby #1126

Closed
KIAnne opened this issue May 27, 2022 · 7 comments
Closed

3.2.5 查询后自动添加了orderby #1126

KIAnne opened this issue May 27, 2022 · 7 comments

Comments

@KIAnne
Copy link

KIAnne commented May 27, 2022

问题描述及重现步骤:

数据库的具体版本

sqlserver 2012

安装的包

freesql 3.2.5

.net framework/. net core? 及具体版本

netcore3.1

image

求助!

@KIAnne
Copy link
Author

KIAnne commented May 27, 2022

明白了。。。没有指定排序字段 会自动把主键加到排序里面

@2881099
Copy link
Collaborator

2881099 commented May 27, 2022

如果默认没有排序,并且表没有聚合索引 ,分页数据会乱。

@2881099
Copy link
Collaborator

2881099 commented May 27, 2022

.OrderBy(g => g.key.TM)

加这行就行了,不加分页出来的数据也会有顺序问题。

@KIAnne
Copy link
Author

KIAnne commented May 27, 2022

@2881099 已经发现了 感谢

@KIAnne
Copy link
Author

KIAnne commented May 27, 2022

@2881099 只不过在第一页的时候他不会提示错误 到第二页的时候才会报出错误 就忽略了

@2881099
Copy link
Collaborator

2881099 commented May 27, 2022

因为第一页直接 TOP 查询,第二页才走的分页

代码是这样写的:

if (_skip > 0)
{
    if (string.IsNullOrEmpty(_orderby))
    {
            var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
            if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
            else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
    }
    sb.Append(_orderby).Append($" \r\nOFFSET {_skip} ROW");
    if (_limit > 0) sb.Append($" \r\nFETCH NEXT {_limit} ROW ONLY");
}

@2881099
Copy link
Collaborator

2881099 commented May 27, 2022

你可以观察监听一下 SQL,我们所有第一页都是 TOP 10,而不需要 row_number() 或者 offset row fetch next

2881099 added a commit that referenced this issue Jun 1, 2022
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

3 participants