diesel.rs 使用 select max 与其他参数

huangapple go评论50阅读模式
英文:

diesel.rs use select max with other arguments

问题

I implemented the MySQL query in Rust, but selecting causes an error. The error message is: "the trait diesel::expression::MixedAggregates<diesel::expression::is_aggregate::Yes> is not implemented for diesel::expression::is_aggregate::No." The error doesn't occur when I don't include progress::onemoreid in the select statement.

英文:

i have implemented the mysql query in rust, but i select causes an error.
This is the error the trait `diesel::expression::MixedAggregates<diesel::expression::is_aggregate::Yes>` is not implemented for `diesel::expression::is_aggregate::No
Whats the problem and how can i fix this?

let sub = progress::table
  .filter(progress::user.eq(1))
  .group_by(progress:: otherid)
  .select((diesel::dsl::max(progress::updated), progress::otherid, progress::onemoreid));
SELECT otherid, onemoreid, MAX(updated) AS newest_updated
    FROM progress
    WHERE user = 1
    GROUP BY otherid

EDIT: the error doesnt occur when i dont have progress::onemoreid in select.

答案1

得分: 1

The relevant part from diesel documentation is the following paragraph:

> Diesel follows postgresql’s group by semantic, this means any column appearing in a group by clause is considered to be aggregated. If a primary key is part of the group by clause every column from the corresponding table is considerd to be aggregated. Select clauses cannot mix aggregated and non aggregated expressions.

This restriction is in place because otherwise your query could return non-deterministic results. After all, what value of onemoreid should be selected when the query produces multiple possible values.

MySQL allows to write such queries, but they can return non-deterministic results there. That means that just some value is returned. Possibly even different values for the execution of the same query using the same data.

英文:

The relevant part from diesel documentation is the following paragraph:

> Diesel follows postgresql’s group by semantic, this means any column appearing in a group by clause is considered to be aggregated. If a primary key is part of the group by clause every column from the corresponding table is considerd to be aggregated. Select clauses cannot mix aggregated and non aggregated expressions.

This restriction is in place because otherwise your query could return non-deterministic results. After all, what value of onemoreid should be selected when the query produces multiple possible values.

MySQL allows to write such queries, but they can return non-deterministic results there. That means that just some value is returned. Possibly even different values for the execution of the same query using the same data.

huangapple
  • 本文由 发表于 2023年4月13日 23:22:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定