【发布时间】: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