Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT inside a COUNT

Tags:

sql

mysql

I would like to embed a SELECT inside a COUNT, but I can't find any examples.

#pseudosql SELECT a AS current_a, COUNT(*) AS b,    COUNT( SELECT FROM t WHERE a = current_a AND c = 'const' ) as d,    from t group by a order by b desc 
like image 842
Issac Kelly Avatar asked Jul 16 '10 16:07

Issac Kelly


People also ask

Can I use select inside count?

SQL SELECT statement can be used along with COUNT(*) function to count and display the data values. The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).

How do you use Select in count?

In SQL, you can make a database query and use the COUNT function to get the number of rows for a particular group in the table. Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; COUNT(column_name) will not include NULL values as part of the count.

How do you condition inside a count in SQL?

SQL COUNT() with HAVING The HAVING clause with SQL COUNT() function can be used to set a condition with the select statement. The HAVING clause is used instead of WHERE clause with SQL COUNT() function.

How do I select a column of counts?

SELECT count(*) as No_of_Column FROM information_schema. columns WHERE table_name ='geeksforgeeks'; Here, COUNT(*) counts the number of columns returned by the INFORMATION_SCHEMA . columns one by one and provides the final count of the columns.


1 Answers

You don't really need a sub-select:

SELECT a, COUNT(*) AS b,    SUM( CASE WHEN c = 'const' THEN 1 ELSE 0 END ) as d,    from t group by a order by b desc 
like image 63
Justin K Avatar answered Sep 24 '22 23:09

Justin K