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

How to bind Vec<i64> arguments for IN operator? #528

Closed
ensonmj opened this issue Jul 17, 2020 · 6 comments
Closed

How to bind Vec<i64> arguments for IN operator? #528

ensonmj opened this issue Jul 17, 2020 · 6 comments

Comments

@ensonmj
Copy link

ensonmj commented Jul 17, 2020

let ids = vec![4353455,234234];
sqlx::query("SELECT * FROM xx WHERE id IN (?)).bind(ids).fetch_all(&mut conn)

seems not work?

@abonander
Copy link
Collaborator

Which database are you using?

@ensonmj
Copy link
Author

ensonmj commented Jul 17, 2020

mysql 5.6; and now I use sqlx master(0.4.0-pre)

@abonander
Copy link
Collaborator

MySQL does not support arrays of values for bind parameters. You would need to generate a query that has as many bind placeholders as values in your array, and then bind each one individually. We don't provide anything to do that right now, but are discussing it in #291.

@ensonmj
Copy link
Author

ensonmj commented Jul 17, 2020

I don't not MySQL protocol detail.
does MySQL check bind value's type?
I try to join the vec and pass str to bind()

let ids = vec![4353455,234234];
let id_str = ids.iter().map(ToString::to_string).collect::<Vec<String>>().join(",");
sqlx::query("SELECT * FROM xx WHERE id IN (?)).bind(id_str).fetch_all(&mut conn)

but it just return record for the first id, seems MySQL try to transfrom id_str(String) to SomeType<i64> and just get the first id。

@abonander
Copy link
Collaborator

That won't work, you just queried MySQL with SELECT * FROM xx WHERE id IN ('4353455,234234') (notice the quotation marks), so it converted id to a string and checked if it equaled '4353455,234234'.

@ensonmj
Copy link
Author

ensonmj commented Jul 17, 2020

so it converted id to a string and checked if it equaled '4353455,234234'.

seems not exactly.
cause I can get one record which id=4353455 return.

finally, have to workaround with let sql = format!("SELECT * FROM xx WHERE id IN ({})", "4353455,234234") , saddly

@ensonmj ensonmj closed this as completed Jul 17, 2020
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

2 participants