Jump to content

MySQL: Two Counts

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
BASHERS33

BASHERS33

    Programmer

  • Members
  • PipPipPipPip
  • 198 posts
In the past I had a query with 2 joins and each join had a count selected in it. Some programmer on another site told me that would not work because of however joins behave where it wouldn't count only the rows from that table, but also returned some other rows from the other join. i don't know... I'm not good at this, I just know what he said and indeed that query did give erroneous results.

So how do I get multiple counts in a query with joins and have them be accurate?

And also does this issue only come up with multiple joins?

What do I do if I have only one join but I need two counts from that joined table? The only thing I have known to do is do a second join on the same table and change the where clause when doing the count, but that would then cause this sisue due to multiple joins again.

In the example from the past I simply got the counts from a different query, but that is not ideal... I put the counts in an associative array and got them back to merge them later o. I may have to do that again, which is not ideal.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Start by looking at what the join creates without COUNT.

You may be able to use a COUNT along with a GROUP BY clause to get a few counts based of various criteria. With two tables, it is extremely likely that you will either need to throw in some DISTINCT options as well.

Ultimately, you may be better off with two distinct queries, however.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog