簡體   English   中英

左外部連接在導軌中

[英]Left outer join in rails

我有兩個對象-客戶和客戶簽到。 每個客戶都有許多簽到,每個簽到屬於一個客戶。

我需要提出一個查詢,該查詢輸出完整的客戶列表以及每月的簽到計數。 我當前的查詢如下所示:

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...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM