Drew
Drew

Reputation: 6872

MySQL Combining Multiple Count Queries

I have the following query:

SELECT COUNT(package) as advanced_count FROM users WHERE package = '2' AND site_url is     NOT NULL;

What I want to do is have 2 more queries to get the basic_count and then the total_count which is basic + advanced.

My other basic query is:

SELECT count(package) as basic_count FROM users WHERE package = '1' AND site_url is NOT NULL

But I'm not sure how to combine the two so it is just one query, and then plus adding the total number in there as well.

I hope someone can point me in the right directions.

Thank you!

Upvotes: 2

Views: 948

Answers (1)

Joe Stefanelli
Joe Stefanelli

Reputation: 135938

SELECT SUM(CASE WHEN package = '2' THEN 1 ELSE 0 END) AS advanced_count,
       SUM(CASE WHEN package = '1' THEN 1 ELSE 0 END) AS basic_count,
       COUNT(*) AS total_count
    FROM users
    WHERE site_url IS NOT NULL
        AND package IN ('1', '2');

Upvotes: 4

Related Questions