我正在尝试在Rails控制器中选择所有重复属性。我想按街道比较每个物业的地址,然后将结果返回给我,以显示在页面上。
我尝试过的事情:
@conflicts = Property.where("SELECT street, COUNT(street) FROM properties GROUP BY street HAVING COUNT(street) >1")
这是我收到的错误:
Mysql2::Error: Operand should contain 1 column(s): SELECT `properties`.* FROM `properties` WHERE (SELECT street, COUNT(street) FROM properties GROUP BY street HAVING COUNT(street) >1)
我知道有人问过许多与我类似的问题,但我需要返回整个属性数组及其所有信息,而不仅仅是街道,而不仅仅是数量的重复项。谢谢!
更新:也尝试过:
@conflicts = Property.where("SELECT street FROM properties GROUP BY street HAVING count(street) > 1")
这给了我
Mysql2::Error: Subquery returns more than 1 row: SELECT `properties`.* FROM `properties` WHERE (SELECT street FROM properties GROUP BY street HAVING count(street) > 1)
我想要的是一张桌子,上面有这样的东西
property_id | street | state | zip
1 | 123 test drive | Arkansas | 12345
2 | 123 test drive | Iowa | 45632
3 | 999 not avenue | Delaware | 55556
并返回属性1和2。
答案 0 :(得分:0)
我不知道RoR查询构建器语法,但是您想要的原始查询是:
SELECT street, COUNT(*)
FROM properties
GROUP BY street
HAVING COUNT(*) > 1