title: How to count number of distinct values when joining multiple tables?
body: <p>I have following database tables </p>
  
  <pre><code>categories
  id, name
  
  products
  category_id, id, product_name, user_id
  
  product_comments
  product_id, id, comment_text, user_id
  </code></pre>
  
  <p>I need a count of number of different users in both products and product_comments tables.  I have got the following query where I select all those categories where there is atleast one product and each product may have zero or some comments ... but what I can't figure out is that how to get the sum of these different user ids .. if it were just from one table I would try COUNT(products.user_id) ... .here is my query .. </p>
  
  <pre><code>SELECT 
  c.*
  FROM categories c
  INNER JOIN products p ON p.category_id = c.id
  LEFT JOIN product_comments pc ON pc.product_id = p.id
  </code></pre>
  
  <p>I need total number of different users IDs from both products and product_comments tables.</p>
  
  <p>I would expect the result data somewhat like below:</p>
  
  <pre><code>Category_id, Category_name, TotalUsers
  1, Test Category, 10
  2, Another Category, 5
  3, Yet another cat, 3
  </code></pre>