Jump to content

Trying to add counts (totals) from another table


svivian

Recommended Posts

I have two tables with the format:

 

page: id / url

error: id / type / pageid / message

 

error.pageid relates to page.id, so there are multiple 'error' tuples for each page

error.type can be 1 (error) or 2 (warning).

 

I want to display a list of pages with an error count and a warning count. I've managed to get the total errors+warnings with this query:

SELECT p.id, p.url, count(e.id) AS tot
FROM page p, error e
WHERE e.pageid=p.id
GROUP BY p.id

 

However I can't find a way to split the total into the two types. I tried this but it takes ages and gives really large numbers (bit in [ ] I've tried with and without):

SELECT p.id, p.url, count(e.id) AS errors, count(w.id) AS warnings
FROM page p, error e, error w
WHERE e.pageid=p.id AND w.pageid=p.id [AND e.pageid=w.pageid]
GROUP BY p.id

 

Maybe I'm missing an extra join or something? Can anyone help?

Link to comment
Share on other sites

Well I can just do two queries, one that gets the errors and one that gets the warnings.

 

However, I have another problem now - anything where there are 0 errors doesn't show up. From a quick bit of searching apparently LEFT JOIN is the way to go in this circumstance, to make sure I get all the pages listed... but I've tried and it makes no difference.

 

Do I have this correct? This query is still getting the total errors+warnings for now:

SELECT p.id, p.url, count(e.id) AS errors
FROM page p
LEFT JOIN error e ON (e.pageid=p.id)
GROUP BY p.id

 

EDIT: scratch that, the above query works fine, but adding

WHERE e.type=1

to the query gets rid of the 0 entries again...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.