Join three tables with group by giving error


Keywords:mysql 


Question: 

There are three tables in the database users, organization_entries and user_invoices and I am trying to Join these three tables and my query is somewhat like this

select users.id , sum(user_invoices.due_amount) , organization_entries.id, organization_entries.createdAt from users INNER JOIN user_invoices ON users.id = user_invoices.customer_id INNER JOIN on users.id = organization_entries.user_id GROUP BY users.id ORDER BY organization_entries.createdAt;

But again and again, I am getting this error -

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'on users.id = organization_entries.user_id GROUP BY users.id ORDER BY organiza' at line 1

I am not able to understand where I am doing things wrong.


1 Answer: 

Update the query as follows, you are missing table in second inner join

select users.id , sum(user_invoices.due_amount) , organization_entries.id, 
organization_entries.createdAt 
from users INNER JOIN user_invoices ON users.id = user_invoices.customer_id 
INNER JOIN organization_entries ON users.id = organization_entries.user_id 
GROUP BY users.id ORDER BY organization_entries.createdAt ;