有没有办法在RoR中运行与以下结果相同的查询?
SELECT SUBSTRING_INDEX(notes, ',' , 1) AS notes FROM <table> WHERE event = 'Bar'
我已经尝试过但是substring_index部分似乎与find_by_sql函数不友好。
更新这是对我试图提出的查询的更准确描述。
SELECT SUBSTRING_INDEX(notes, ',', 1) notes, COUNT(*) cnt FROM <table> WHERE event = 'Foo' GROUP BY SUBSTRING_INDEX(notes, ',', 1) ORDER BY COUNT(*) DESC
我接近这个电话:
Foo.where("event = 'Foo'").group('notes').order('count_all').count()
但显然不太正确。
答案 0 :(得分:0)
Foo.select("SUBSTRING_INDEX(notes, ',', 1) AS notes, COUNT(*) AS cnt").
where(:event => "Foo").group('notes').order('cnt DESC')