繁体   English   中英

使用通配符对阵列列进行Activerecord查询

[英]Activerecord query against array column using wildcard

所以,假设我有一个带阵列列phonesCustomer模型。 通过手机找到所有客户非常容易

Customer.where('? = ANY(phones)', '+79851234567')

但是,当我想找到类似于给定的手机的客户时,我无法弄清楚如何使用LIKE和通配符,例如:

Customer.where('ANY(phones) LIKE ?', '+7985%')

我正在使用PostgreSQL 9.5和Rais 4.2

有任何想法吗?

我认为,首先,最好使用带有customer_id,phone_number字段的第二台手机。 我认为这是更多的轨道方式)。 通过这种方式,您可以使用此查询

Phone.where("phone_number LIKE ?", '%PART%').first.customer

如果您在某些文本字段中序列化数组(例如JSON),则应在模式的两端使用%:

Customer.where('phones LIKE ?', '%+7985%')

如果数据库中有数组,则应使用unnest()函数将数组扩展为一组行。

你能试试吗?

Customer.where("array_to_string(phones, ', ') like ?", '+7985%')

我相信这会奏效。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM