[英]How to write sql request through Active Record Query Interface with joins two tables where condition - array of ids
这是没有 Rails 型号的纯 ActiveRecord,只需连接到另一个 mysql db。 我试着写一个这样的请求:
Product.joins('inner join oc_product_description on oc_product_description.product_id = oc_product.product_id').where('product_id =?',[64,66,70,71])
并得到错误ActiveRecord::StatementInvalid (Mysql2::Error: Column 'product_id' in where clause is ambiguous)
当我添加这样的唯一表名时.where('oc_product.product_id =?',[64,66,70,71])
我收到另一个错误ActiveRecord::StatementInvalid (Mysql2::Error: Operand should contain 1 column(s))
= 用于查找单个值。 如果要使用数组,则 sql 语法为
.where('oc_product.product_id IN?',[64,66,70,71])
不确定 ActiveRecord 是如何工作的,但你可以试试这个。
将IN
用于多个值(数组):
Product.joins('inner join oc_product_description on oc_product_description.product_id = oc_product.product_id')
.where('oc_product.product_id IN (?)',[64,66,70,71])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.