[eluser]Wazzu[/eluser] Filter by related table sum()
Hi all, this is my case:
- table users (id, name)
- table invoices (id, user_id, total)
Each user has several invoices
I want to select all users where total invoiced is greater than a value, let's put $100
This is my SQL query:
Code:
SELECT users.*, sum(invoices.total) as invoiced
FROM users
LEFT JOIN invoices ON (users.id = invoices.user_id)
WHERE invoiced > 100
GROUP BY users.id
I'm trying with where_related(), but can't get mysql's sum() function working.
How should I manage this using DMZ?