简体   繁体   English

使用 rust 柴油时如何实现小于查询

[英]how to implement a less than query when using rust diesel

I am using rust diesel to query the PostgreSQL 13 database,now I want to do a query like this:我正在使用 rust 柴油查询 PostgreSQL 13 数据库,现在我想做这样的查询:

select *
from fav
where playlist_id = 1
and play_count < 100

what I am doing using diesel rust code like this:我在做什么使用柴油 rust 代码,如下所示:

let playlist_records = favorites.filter(source_id.eq(req_song_id))
        .filter(play_count<100)
        .limit(1)
        .load::<QueryFavorites>(&connection)
        .expect("Error loading favorite");

seems did not work as expect, I read the diesel document but did not found the less than query demo, what should I do to implement an less than query using rust diesel?似乎没有按预期工作,我阅读了柴油文档但没有找到小于查询演示,我应该怎么做才能使用 rust 柴油实现小于查询?

you need to use diesel's methods for comparison within the filter call.您需要在过滤器调用中使用柴油的方法进行比较。

I just quickly googled "rust diesel filter" to check the official documentation, and there I found the le method: https://docs.diesel.rs/diesel/expression_methods/trait.ExpressionMethods.html#method.lt我只是快速搜索“锈柴油过滤器”以查看官方文档,并在那里找到了le方法: https://docs.diesel.rs/diesel/expression_methods/trait.ExpressionMethods.html#method.lt

Their example:他们的例子:

let data = users
    .select(name)
    .filter(id.lt(2))
    .first::<String>(&connection)?;

This filters for everything where id < 2 .这会过滤id < 2的所有内容。

So in your case, just write .filter(play_count.lt(100)) I think.所以在你的情况下,我想只写.filter(play_count.lt(100))

Rust 如何实现 diesel::Insertable<div id="text_translate"><p> 我正在尝试在 Rust 中实现特性diesel::Insertable<table 。</p><p> 在mail.rs</p><pre> diesel::insert_into(mailing_list).values( email // This is a String ).execute(connection);</pre><p> 这是因为以下代码导致此错误:</p><pre> error[E0277]: the trait bound `std::string::String: diesel::Insertable<table>` is not satisfied --> src/mail.rs:18:10 | 17 | diesel::insert_into(mailing_list).values( | ------ required by a bound introduced by this call 18 | email | ^^^^^ the trait `diesel::Insertable<table>` is not implemented for `std::string::String`</pre><p> 多看一下 Diesel 的文档,我相信我应该让自己成为一个派生可插入的结构</p><p>在models.rs</p><pre> #[derive(Insertable)] pub struct NewSubscriber { pub email: String }</pre><p> 但后来我得到</p><pre>but im getting this error error[E0433]: failed to resolve: use of undeclared crate or module `new_subscribers` --> src/models.rs:13:12 | 13 | pub struct NewSubscriber { | ^^^^^^^^^^^^^ use of undeclared crate or module `new_subscribers`</pre><p> 我很困惑为什么编译器说它找不到包含我正在尝试定义的结构的板条箱或模块。</p></div><table></table> - Rust how to implement diesel::Insertable<table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用rust柴油时如何进行计数查询 - how to do a count query when using rust diesel 如何使用锈柴油进行查询分组 - how to using rust diesel to do the group by query 如何使用锈柴油做全文查询 - how to using rust diesel to do the full text query Rust 如何实现 diesel::Insertable<div id="text_translate"><p> 我正在尝试在 Rust 中实现特性diesel::Insertable<table 。</p><p> 在mail.rs</p><pre> diesel::insert_into(mailing_list).values( email // This is a String ).execute(connection);</pre><p> 这是因为以下代码导致此错误:</p><pre> error[E0277]: the trait bound `std::string::String: diesel::Insertable<table>` is not satisfied --> src/mail.rs:18:10 | 17 | diesel::insert_into(mailing_list).values( | ------ required by a bound introduced by this call 18 | email | ^^^^^ the trait `diesel::Insertable<table>` is not implemented for `std::string::String`</pre><p> 多看一下 Diesel 的文档,我相信我应该让自己成为一个派生可插入的结构</p><p>在models.rs</p><pre> #[derive(Insertable)] pub struct NewSubscriber { pub email: String }</pre><p> 但后来我得到</p><pre>but im getting this error error[E0433]: failed to resolve: use of undeclared crate or module `new_subscribers` --> src/models.rs:13:12 | 13 | pub struct NewSubscriber { | ^^^^^^^^^^^^^ use of undeclared crate or module `new_subscribers`</pre><p> 我很困惑为什么编译器说它找不到包含我正在尝试定义的结构的板条箱或模块。</p></div><table></table> - Rust how to implement diesel::Insertable<table> Rust柴油Postgres如何添加分页查询 - Rust diesel Postgres how to add pagination to query 如何使用生锈柴油获得柱的平均值? - How to get average of a column using rust diesel? 如何使用柴油进行 IN 查询? - How to do a IN query using diesel? Rust/Rocket/Diesel - 如何使用 rocket_sync_db_pools 查询 MySqlDatabase - Rust/Rocket/Diesel - How can I query a MySqlDatabase using rocket_sync_db_pools 如何在单独的 function 中正确移动 Rust Diesel 查询? - How to properly move Rust Diesel query in a separate function? Rust柴油有条件过滤查询 - Rust diesel conditionally filter a query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM