SQL: count number of distinct values in every column
count, distinct, mysql, sql
Solution
I appreciate all of the responses. I think the solution that will work best for me in this situation (counting the number of distinct values in each column of a table from an external program that has no knowledge of the table except its name) is as follows:
Run "describe table1" and pull out the column names from the result.
Loop through the column names and create the query to count the distinct values in each column. The query will look something like "select count(distinct columnA), count(distinct columnB), ... from table1".
Problem
I need a query that will return a table where each column is the count of distinct values in the columns of another table. I know how to count the distinct values in one column: ``` select count(distinct columnA) from table1; ``` I suppose that I could just make this a really long select clause: ``` select count(distinct columnA), count(distinct columnB), ... from table1; ``` but that isn't very elegant and it's hardcoded. I'd prefer something more flexible.