View Single Post
Join Date: May 2008
Posts: 3
Reputation: recursiveNugget is an unknown quantity at this point 
Solved Threads: 0
recursiveNugget's Avatar
recursiveNugget recursiveNugget is offline Offline
Newbie Poster

Re: Problem using JOIN with six tables

 
0
  #3
May 10th, 2008
Thanx for your reply hollystyles,

I've made a typo... the second column name on tables tDocumentsA ... tDocumentsD should read contact_id and not user_id, so tDocumentsA.user_id = tUsers.contact_id should be correct. Sorry about that.

I've used INNER JOIN as you said, but now I'm only getting one row.

This is my query:

  1. SELECT tContacts.name, COUNT(tDocumentsA.id) AS 'Doc_A', COUNT(tDocumentsB.id) AS 'Doc_B', COUNT(tDocumentsC.id) AS 'Doc_C', COUNT(tDocumentsD.id) AS 'Doc_D'
  2. FROM tUsers LEFT OUTER JOIN
  3. tContacts ON tContacts.id = tUsers.contact_id INNER JOIN
  4. tDocumentsA ON tDocumentsA.user_id = tUsers.contact_id INNER JOIN
  5. tDocumentsB ON tDocumentsB.user_id = tUsers.contact_id INNER JOIN
  6. tDocumentsC ON tDocumentsC.user_id = tUsers.contact_id INNER JOIN
  7. tDocumentsD ON tDocumentsD.user_id = tUsers.contact_id
  8. WHERE (tUsers.location = '1')
  9. GROUP BY tContacts.name

And this is what I'm getting (only one contact):

  1. name Doc_A Doc_B Doc_C Doc_D
  2. ------------------------------------------------------------
  3. contact5 16 16 16 16

When using LEFT OUTER JOIN I get the right people, but incorrect numbers (In the example above, using INNER JOIN, I'm also getting the incorrect numbers).

contact5 should have (Doc_A ... Doc_D): 4, 1, 2, 2. Instead, I'm getting: 16, 16, 16, 16. It seems to be multiplying the column values (4 x 1 x 2 x 2 = 16)...
Reply With Quote