我有两个对象 - 客户和客户签到。每个客户有很多签到,每个签到属于一个客户。
我需要想出一个查询来输出完整的客户列表以及他们每月的签到次数。我当前的查询如下所示:
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')")
此查询未列出所有客户。我的代码有什么问题?
最佳答案
它不会列出所有客户,因为您只搜索 account_id 为 139 的客户。
替换
customer = Customer.where(account_id: 139)...
与
customer = Customer...
关于mysql - 左外加入 rails ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30600615/