Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counting from different tables

Status
Not open for further replies.

BB101

Programmer
May 23, 2001
337
GB
I have a problem getting counts from 2 different tables, via joins from the parent table.

At the moment, I am doing this:

Code:
SELECT q.*, q.id as q_id, t.town, c.category,
  count(qa.id) AS answers
FROM qq_local q
  LEFT JOIN qa_local qa
    ON q.id = qa.q_id
      AND qa.active = 1
  LEFT JOIN towns t
    ON t.id = q.town_id
  LEFT JOIN q_category c
    ON q.category_id = c.id
  GROUP BY q.q
  ORDER BY q.q_id DESC

I foolishly expected this to work:

Code:
SELECT q.*, q.id as q_id, t.town, c.category,
  count(qa.id) AS answers,
  count(qe.id) AS waiting
FROM qq_local q
  LEFT JOIN qa_local qa
    ON q.id = qa.q_id
      AND qa.active = 1
  LEFT JOIN qe_email qe
    ON q.id = qe.q_id
  LEFT JOIN towns t
    ON t.id = q.town_id
  LEFT JOIN q_category c
    ON q.category_id = c.id
  GROUP BY q.q
  ORDER BY q.q_id DESC

Unfortunatly, the "answers" and "waiting" numbers are always identical; because the joins cannot be counted correctly.

Does anyone know of a solution??

--BB
 
Anyone? This is quite urgent...

--BB
 
In answer to my own question, using SELECT count(DISTINCT x) worked.

--BB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top