0
我有表invoices
與字段customer_id
和其他字段。我需要每個用戶選擇的購買次數。通過invoices.id
代替invoices.customer_id
Django GROUP_BY通過.annotate()
Invoice.objects.annotate(buy_count=Count('customer')).all()
但這碼組和返回錯誤的結果:在SQL它應該是這樣的:
SELECT username, COUNT('customer_id') FROM `invoices`
LEFT JOIN `auth_user` ON `auth_user`.id = `invoices`.customer_id
GROUP BY `customer_id`, username
在Django中我嘗試。