Select more than one account in mysql

Is it possible in MySQL to do something like this:

SELECT COUNT(*) as totalcount, COUNT(*) WHERE foo IS NULL as conditional_count FROM baz 

i.e. get two accounts, one of everything and one of the things matching the WHERE clause in one select?

+6
source share
5 answers

This will work if your database supports the CASE WHEN statement, otherwise you will still get the main idea.

 SELECT COUNT(*), SUM(CASE WHEN FOO IS NULL THEN 1 ELSE 0 END) AS COUNT_CONDITIONAL FROM baz 
+11
source

it should look like this: SELECT COUNT(*) as totalcount, (SELECT COUNT(*) WHERE foo IS NULL) as conditional_count FROM baz this is the only query, but with a subquery inside

+2
source

SELECT count () as totalcount, (select COUNT () FROM baz WHERE foo IS NULL) as conditional_count FROM baz;

I cannot put a star (*) between the brackets of count !!! I put it, but I can’t display it.

+1
source

try with UNION (not verified)

 SELECT COUNT(*) as totalcount FROM baz UNION SELECT COUNT(*) WHERE foo IS NULL as conditional_count FROM baz 
0
source

I do not think that's possible. I believe you will need two queries.

 Select count(*) from baz Select count(foo) from baz where foo is null 
-1
source

Source: https://habr.com/ru/post/889354/


All Articles