【问题标题】:Left outer join in rails左外连接导轨
【发布时间】:2015-08-16 12:19:04
【问题描述】:

我有两个对象 - 客户和客户签到。每个客户有很多签到,每个签到属于一个客户。

我需要提出一个查询,输出完整的客户列表以及他们每月的签到次数。我当前的查询如下所示:

customer = Customer.where(account_id: 139).joins('LEFT JOIN
    customer_checkins on customer_checkins.customer_id =
    customers.id').uniq.select("COUNT(*) as count, customers.created_at as
    created_at, customers.name, customers.telephone, customers.mobile,
    customers.email, customers.gender, customers.city,
    customers.birthday,date_format(customer_checkins.created_at, '%b') as
    'month', customers.id").group("customer_checkins.customer_id,
    date_format(customer_checkins.created_at, '%b %Y')")

此查询未列出所有客户。我的代码有什么问题?

【问题讨论】:

  • 假设您没有收到错误,您是否设置了 default_scope?如果是,您可以使用 unscoped(即 Customer.unscoped.where ..)

标签: mysql ruby-on-rails join left-join


【解决方案1】:

它没有列出所有客户,因为您只搜索 account_id: 139 的客户。

替换

customer = Customer.where(account_id: 139)...

与

 customer = Customer...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 2011-10-01
    • 2016-10-02
    • 1970-01-01
    • 2011-09-10
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多