我正在尝试选择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)
我想要的是有一张这样的table
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。
1条答案
按热度按时间okxuctiv1#
我不知道ror query builder语法,但您需要的原始查询是: