query = <<-sql_statement
SELECT * FROM
(
select * from `my_table` order by timestamp desc
) as my_table_tmp
group by catid
order by nid desc
sql_statement
result = MyTable.connection.execute(query)
您也可以使用ActiveRecord::Base.connection.execute(query)
.
要遍历返回使用的记录result.each
。
示例(来自 mysql2 gem repo):
results.each do |row|
# conveniently, row is a hash
# the keys are the fields, as you'd expect
# the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
# Here's an otter: http://farm1.static.flickr.com/130/398077070_b8795d0ef3_b.jpg
end