0

I am trying to run the following query to return a list of all months with the count of English and Scottish users. I am not sure how to run multiple Count queries along side each other.

SELECT month(Date) as month
     , COUNT(*) as scottishCount 
  FROM users userTable 
 WHERE year(Date) = 2015 
   AND userTable.UserID in 
 (select nationsTable.UserID 
    from users nationsTable 
   where nationsTable.Nation = 'Scotland'), COUNT(*) as englishCount FROM users userTable WHERE year(Date)=201 AND userTable.UserID in (select nationsTable.UserID from nations nationsTable where nationsTable.Nation= 'England') GROUP BY month(DateClicked);

Thanks in advance,

Fred

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Fred Smith
  • 33
  • 4

1 Answers1

0

You must move your conditions to the SELECT clause in order to count conditionally. MySQL treats true as 1 and false as 0, so you can use SUM to count matches. It doesn't seem necessary to select from the users table multiple times:

SELECT
  MONTH(date) AS month, 
  SUM(nation = 'Scotland') AS scottish_count,
  SUM(nation = 'England') AS english_count
FROM users
WHERE YEAR(date) = 2015 
GROUP BY MONTH(date)
ORDER BY MONTH(date);
Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73