3 Replies - 345 Views - Last Post: 18 March 2011 - 08:31 AM

#1 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

SUM of COUNTS, or something similar?

Posted 16 March 2011 - 07:59 AM

Hey all

This may be difficult to explain so please bear with me. I am running a query that is meant to test whether two records are related in some way, there are three different types of relationships they have and I only need to see if one exists. This is accomplished using a few left joins and a subquery and the end result is three rows that are either 0 or 1. The SELECT looks like this:
 SELECT COUNT(table1.field),COUNT(table2.field),COUNT(table3.field)



The method that calls this has to do a check on each one e.g if count1 > 0 || count2 > 0 etc. I was just wondering if there was a way to get the total directly in MySQL, I know SUM won't work in this case and can't seem to find anything appropriate. Seems kind of silly to go through this just to avoid a few or's in my conditional but it just seems sloppy to me.

Does anyone heave any suggestions?

This post has been edited by Jstall: 16 March 2011 - 07:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: SUM of COUNTS, or something similar?

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: SUM of COUNTS, or something similar?

Posted 16 March 2011 - 08:12 AM

Well I did come up with a solution of sorts, after I made the post I thougt about CONCAT and that does do what I want, sort of.

SELECT CONCAT(COUNT(table1.field),COUNT(table2.field),COUNT(table3.field)) as num



Will return a string(naturally) that will look like 100,101,001 etc. Now since in the method that uses this query I am only comparing the results to be great than zero this will work and suit my needs. Still interested if anyone had any other solutions though. Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 438
  • View blog
  • Posts: 792
  • Joined: 17-June 08

Re: SUM of COUNTS, or something similar?

Posted 16 March 2011 - 11:19 AM

Why not just add them up?
SELECT COUNT(table1.field) + COUNT(table2.field) + COUNT(table3.field) AS total

Was This Post Helpful? 1
  • +
  • -

#4 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: SUM of COUNTS, or something similar?

Posted 18 March 2011 - 08:31 AM

Ahh of course! Haha now I feel dumb, thanks :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1