将 SQL 查询转换为 Ruby 帮助 [英] Convert SQL query to Ruby help
本文介绍了将 SQL 查询转换为 Ruby 帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要查询我的数据库表,以找出哪位员工拥有最多的与其相关的支持票.我可以使用这个 MySQL 查询很好地做到这一点:
I need to query my database table to find which employee has the most support tickets related to them. I can do this just fine using this MySQL query:
SELECT employee_id, COUNT(id) AS number_of_tickets FROM tickets GROUP BY employee_id ORDER BY number_of_tickets DESC LIMIT 1;
如何在 Ruby-on-Rails 中编写这个?
How would write this in Ruby-on-Rails?
非常感谢您的帮助.
我使用 Ruby 1.8.6 版、Rails 2.2.2 版和 MySQL Server 5.0 版.
推荐答案
试试这个:
Ticket.find(:all, :select => 'employee_id, count(id) as number_of_tickets',
:group => 'employee_id' , :order => "number_of_tickets Desc", :limit => 1 )
或者直接使用:
Ticket.find_by_sql('select...... ' )
这篇关于将 SQL 查询转换为 Ruby 帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文