-2

我有两个名为lines and的表attribute_values。从中我想从具有以下 MySQL 查询中给出的条件namelines表中选择字段:

select distinct a.name 
from lines a join attribute_values b 
where a.advertiser_id = 280 
  and a.id = b.line_id 
  and b.name in (11, 18) 
  and b.value != 0;

如何使用 Ruby 代码编写此查询?

4

3 回答 3

0

假设行 has_many attribute_values

尝试这个

Line.joins(:attribute_values).select("DISTINCT(lines.name)").where("lines.advertiser_id = ? AND attribute_values.name IN (?) AND attribute_values.value != ?", 280, [11,18], 0)
于 2013-08-02T11:41:45.837 回答
0

要获得独特的价值,您必须申请uniq.

Lines.joins(:attribute_values).where('lines.advertiser_id = ? AND attribute_values.name in ? AND attribute_values.value <> ?', 280, [11, 18], 0).uniq
于 2013-08-02T11:42:03.727 回答
0
Line.joins("a LEFT JOIN attribute_values b ON a.id = b.line_id").select("DISTINCT(a.name)").where("b.value != ? AND b.name in (?) AND a.advertiser_id = ?", 0, [11, 18], 280)
于 2013-08-02T11:42:37.790 回答