Home > Net >  MySQL: How do I merge two count queries?
MySQL: How do I merge two count queries?

Time:08-05

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

Please do like this way

SELECT 
  (SELECT count(url) as t1 from shop_links) as cnt1,
  (SELECT count(url) as t2 from shop_links where status = 3) as cnt2 
  • Related