左外连接在轨道中

时间:2015-06-02 15:42:04

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

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

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

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')")

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

1 个答案:

答案 0 :(得分:0)

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

替换

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

 customer = Customer...