gpt4 book ai didi

mysql - 如何使用 join 和 group by 获取 Rails 中最后输入的记录字段?

转载 作者:行者123 更新时间:2023-11-29 05:47:22 25 4
gpt4 key购买 nike

我正在使用 rails 5.1 和 ruby​​ 2.4,我被困在 rails 事件记录查询中,我已经在 SQL 中创建了它,但无法以 rails 方式复制它。请建议正确的语法,在此先感谢。Sql查询是:

select jobs.id, users.first_name, users.last_name, users.email, MAX(comments.created_at) from comments left join jobs on jobs.id = comments.job_id left join users on users.id = jobs.user_id group by jobs.id;

在rails中对应的查询是:

Comment.joins("left join jobs on jobs.id = comments.job_id").joins("left join users on users.id = jobs.user_id").select("jobs.id, users.first_name, users.last_name, users.email, max(comments.created_at)").group_by("jobs.id")

出现以下错误:

Completed 500 Internal Server Error in 151ms (ActiveRecord: 6.1ms)

ArgumentError (wrong number of arguments (given 1, expected 0)):

输出应该是这样的:

+-------+------------+-----------+------------------------+--------------------------+
| id | first_name | last_name | email | MAX(comments.created_at) |
+-------+------------+-----------+------------------------+--------------------------+
| 63606 | vidur | punj | vidur.punj@hotmail.com | 2019-04-18 05:19:30 |
| 63608 | vidur | punj | vidur.punj@hotmail.com | 2019-04-26 09:22:21 |
| 63610 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 12:27:24 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 12:15:36 |
| 63684 | vidur | punj | vidur.punj@hotmail.com | 2019-10-15 11:46:45 |
| 63690 | vidur | punj | vidur.punj@hotmail.com | 2019-06-12 05:29:04 |
| 63694 | vidur | punj | vidur.punj@hotmail.com | 2019-05-23 04:03:01 |
| 63700 | vidur | punj | vidur.punj@hotmail.com | 2019-05-20 10:55:31 |
| 63701 | vidur | punj | vidur.punj@hotmail.com | 2019-10-19 10:07:42 |
| 63704 | vidur | punj | vidur.punj@hotmail.com | 2019-06-13 09:52:50 |
| 63705 | vidur | punj | vidur.punj@hotmail.com | 2019-06-11 11:16:35 |
| 63706 | vidur | punj | vidur.punj@hotmail.com | 2019-06-11 11:15:21 |
| 63708 | vidur | punj | vidur.punj@hotmail.com | 2019-06-13 12:44:19 |
| 63709 | vidur | punj | vidur.punj@hotmail.com | 2019-06-11 11:13:48 |
| 63717 | vidur | punj | vidur.punj@hotmail.com | 2019-06-17 09:42:35 |
| 63719 | vidur | punj | vidur.punj@hotmail.com | 2019-06-17 10:38:51 |
| 63726 | vidur | punj | vidur.punj@hotmail.com | 2019-06-20 10:55:44 |
| 63727 | vidur | punj | vidur.punj@hotmail.com | 2019-06-25 04:57:45 |
| 63741 | vidur | punj | vidur.punj@hotmail.com | 2019-07-22 06:51:53 |
| 63778 | vidur | punj | vidur.punj@hotmail.com | 2019-09-13 11:30:56 |
| 63801 | vidur | punj | vidur.punj@hotmail.com | 2019-09-16 06:55:08 |
| 63802 | vidur | punj | vidur.punj@hotmail.com | 2019-09-17 03:56:27 |
| 63815 | vidur | punj | vidur.punj@hotmail.com | 2019-10-30 05:43:07 |
| 63821 | vidur | punj | vidur.punj@hotmail.com | 2019-10-15 10:40:54 |
| 63839 | vidur | punj | vidur.punj@hotmail.com | 2019-09-23 11:07:30 |
| 63898 | vidur | punj | vidur.punj@hotmail.com | 2019-10-29 07:12:15 |
| 63916 | vidur | punj | vidur.punj@hotmail.com | 2019-10-15 12:51:06 |
+-------+------------+-----------+------------------------+--------------------------+

代替:

+-------+------------+-----------+------------------------+--------------------------+
| id | first_name | last_name | email | max(comments.created_at) |
+-------+------------+-----------+------------------------+--------------------------+
| 63606 | vidur | punj | vidur.punj@hotmail.com | 2019-04-18 05:18:27 |
| 63606 | vidur | punj | vidur.punj@hotmail.com | 2019-04-18 05:19:30 |
| 63608 | vidur | punj | vidur.punj@hotmail.com | 2019-04-18 11:06:48 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 05:33:57 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:04:16 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:49:08 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:49:49 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:50:41 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:51:27 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:52:17 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:52:48 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:53:31 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:53:46 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:54:07 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:54:27 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:54:45 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:55:28 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 06:58:00 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 08:41:57 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 09:08:07 |
| 63611 | vidur | punj | vidur.punj@hotmail.com | 2019-04-22 09:08:34 |

最佳答案

我认为这是您的 group_by。那是一个 Vanilla ruby 枚举。您是否尝试过 .group('jobs.id')group 是一个 activerecord 查询助手,它将运行 sql group by 子句

关于mysql - 如何使用 join 和 group by 获取 Rails 中最后输入的记录字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58621040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com