[英]Activerecord query against array column using wildcard
所以,假設我有一個帶陣列列phones
的Customer
模型。 通過手機找到所有客戶非常容易
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.