title: How to get sum from third relation table
body: <p>I'm working on a expenses tracking system and stuck in some part.</p>
  
  <p>I have 3 tables in database.</p>
  
  <p>Table 1(projects)</p>
  
  <p><a href="https://i.stack.imgur.com/21TMN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/21TMN.png" alt="enter image description here"></a></p>
  
  <p>Table 2(category)</p>
  
  <p><a href="https://i.stack.imgur.com/4B2a7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4B2a7.png" alt="enter image description here"></a></p>
  
  <p>Table 3(expenses)</p>
  
  <p><a href="https://i.stack.imgur.com/tLvbJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tLvbJ.png" alt="enter image description here"></a></p>
  
  <p>tblProjects relate to tblCategory with 1:1, tblExpenses relate to tblProjects also with 1:1.</p>
  
  <p>Now, I'm trying to get the sum of each category. For instance I want to know what is the total amount spent for Trip, Sports etc.</p>
  
  <p>I try to get with below query, but it returns me wrong data</p>
  
  <pre><code>SELECT category.cat_title as Category, (select sum(expenses.exp_amount) 
  from expenses
  where expenses.projects_id = projects.proj_id) as Total_Expenses                
  from category inner join projects on projects.proj_cat = category.cat_id 
  group by category.cat_id
  </code></pre>