gpt4 book ai didi

ruby-on-rails - Rails ActiveRecord :joins with LEFT JOIN instead of INNER JOIN

转载 作者:行者123 更新时间:2023-12-03 05:23:54 26 4
gpt4 key购买 nike

我有这个代码

User.find(:all, :limit => 10, :joins => :user_points,
:select => "users.*, count(user_points.id)", :group =>
"user_points.user_id")

生成以下sql

SELECT users.*, count(user_points.id) 
FROM `users`
INNER JOIN `user_points`
ON user_points.user_id = users.id
GROUP BY user_points.user_id
LIMIT 10

除了 User.find_by_sql 并手动输入查询之外,是否可以使用 LEFT JOIN 而不是 INNER JOIN?

最佳答案

你可以试试这个

User.find(:all, limit: 10,
joins: "LEFT JOIN `user_points` ON user_points.user_id = users.id" ,
select: "users.*, count(user_points.id)",
group: "user_points.user_id")

关于ruby-on-rails - Rails ActiveRecord :joins with LEFT JOIN instead of INNER JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1509692/

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