Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL: How do I merge two count queries?

How can I run these queries as a single query?

SELECT count(url) as t1 from shop_links 

and

SELECT count(url) as t2 from shop_links where status = 3 

I would like t1 and t2 in the end

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

SELECT
    COUNT(url)                  AS t1,
    COUNT(IF(status = 3, 1, NULL)) AS t2
FROM shop_links

If records status is 3, the IF will return value for COUNT() to count, if it is not 3 it returns NULL with COUNT will skip counting that record

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading