Dolphin
Dolphin

Reputation: 38733

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:

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

what I am doing using diesel rust code like this:

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?

Upvotes: 1

Views: 1375

Answers (1)

cadolphs
cadolphs

Reputation: 9617

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

Their example:

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

This filters for everything where id < 2.

So in your case, just write .filter(play_count.lt(100)) I think.

Upvotes: 2

Related Questions