MySQL 在查询中使用数组

标签 mysql ruby-on-rails ruby

这是我的状态代码数组 = [1,2,3,4,5],在我的 claim 表中有 status_code。我在那里存储状态代码。现在我想找到那些状态代码为 [1,2,4] 的 Claim。那我怎么才能找到。

我写了这个查询:Claim.where("status_code IN (1,2)") 有效但我需要传递数组。那么如何在“In”查询中传递数组。

感谢帮助。

最佳答案

假设status_code 是整型。可以直接在where子句中使用数组。

array = [1,2,3,4,5]
Claim.where(status_code: array)

array = [1,2,3,4,5]
Claim.where('claims.status_code IN (?)', array)

关于MySQL 在查询中使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22919601/

相关文章:

java - 如何基于特定的资源路径进行尽可能公平的负载均衡

ruby-on-rails - Resque 调度程序将哈希参数传递给 Job

ruby-on-rails - 你如何撤消 bundle install --without

ruby-on-rails - 类型错误 : wrong argument type Class (expected Module) with Controller concern

mysql - 数据迁移: Query to get data from JOIN tables

mysql - 将信息发送到 Dashing 中的小部件

ruby-on-rails - Emacs 做 ruby​​ on rails 有哪些有用的包?

mysql - 如何在 Azure 门户中找到 ClearDB MySQL 数据库?

mysql - 从表中消除排列

php - 从数据库中获取随机数据