Search code examples
mysqlrustrust-sqlx

How to query using an IN clause and a `Vec` as parameter in Rust sqlx for MySQL?


Note: this is a similar but NOT duplicate question with How to use sqlx to query mysql IN a slice?. I'm asking for the Rust one.

This is what I try to do.

let v = vec![..];
sqlx::query("SELECT something FROM table WHERE column IN (?)").bind(v)
 ...

Then I got the following error

the trait bound `std::vec::Vec<u64>: sqlx::Encode<'_, _>` is not satisfied

Solution

  • Answer is in first on FAQ https://github.com/launchbadge/sqlx/blob/master/FAQ.md

    How can I do a SELECT ... WHERE foo IN (...) query? In 0.6 SQLx will support binding arrays as a comma-separated list for every database, but unfortunately there's no general solution for that currently in SQLx itself. You would need to manually generate the query, at which point it cannot be used with the macros.