简体   繁体   English

在MySQL中返回列名称及其不同值的计数

[英]Return column names and count of their distinct values in MySQL

I am looking for a dynamic MySQL query which would count the distinct values in each column of a table and which would also tell me which of the columns contain Null values. 我正在寻找一个动态的MySQL查询,该查询将对表的每一列中的不同值进行计数,并且还会告诉我哪些列包含Null值。
Here is the sample table, I used 'db_name' as database name and 'table_name' as table name: 这是示例表,我使用“ db_name”作为数据库名称,并使用“ table_name”作为表名称:

+------+------+------+------+------+
| Col1 | Col2 | Col3 | Col4 | Col5 |
+------+------+------+------+------+
| a    | d    | j    | o    | q    |
| b    | e    | k    | o    | r    |
| c    | f    | l    | o    | NULL |
| a    | g    | NULL | p    | t    |
| b    | h    | m    | NULL | r    |
| a    | i    | n    | p    | s    |
+------+------+------+------+------+

This is the result set that I would like to get: 这是我想要得到的结果集:

+----------+---------------+---------------+
| Col Name | Unique values | Contains Null |
+----------+---------------+---------------+
| Col1     |             3 | FALSE         |
| Col2     |             6 | FALSE         |
| Col3     |             6 | TRUE          |
| Col4     |             3 | TRUE          |
| Col5     |             5 | TRUE          |
+----------+---------------+---------------+

So far this is what I have managed to figure out: 到目前为止,这是我设法弄清楚的:

-- list all the column names

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'table_name';

-- count the distinct values in a column

SELECT COUNT(DISTINCT Col1) Col1 
FROM table_name;

-- tell if a column contains any Null

SELECT
  (CASE WHEN (SUM(CASE WHEN Col1 IS NULL THEN 1 ELSE 0 END)) > 0 THEN 'TRUE' 
  ELSE 'FALSE' END) 'Contains Null'
FROM table_name;

-- combining the queries

SELECT
  (SELECT COLUMN_NAME
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_NAME = 'table_name' AND COLUMN_NAME = 'Col1') 'Col Name', 
  (SELECT COUNT(DISTINCT Col1) 
  FROM table_name) 'Unique values',
  (SELECT (CASE WHEN (SUM(CASE WHEN Col1 IS 
  NULL THEN 1 ELSE 0 END)) > 0 THEN 'TRUE' ELSE 'FALSE' END)
  FROM table_name) 'Contains Null';

Now, I assume I would need to build a loop that goes through each column and unifies the records returned by the query or inserts them into a new table. 现在,我假设我需要构建一个遍历每一列的循环,并统一查询返回的记录或将它们插入新表中。 The problem is, I am relatively new to SQL and I am not really familiar with loops and variables yet. 问题是,我对SQL比较陌生,对循环和变量还不是很熟悉。

I found a few questions similar to mine, but none of them gave me a clear answer: 我发现了一些类似于我的问题,但没有一个给我一个明确的答案:

SQL Server count number of distinct values in each column of a table SQL Server计算表的每一列中不同值的数量

Return column name and distinct values 返回列名称和不同的值

SQL: count number of distinct values in every column SQL:计算每列中不同值的数量

I just needed thing similiar to this question (get count of all table distinct values and easy way to get it from any table using only SQL) so I've made it on this way. 我只需要与此问题类似的东西(获取所有表的不同值的计数,以及仅使用SQL从任何表中获取它的简单方法),因此我就采用了这种方式。 Hopefully it may help somebody in cases like this one. 希望它可以在这种情况下对某人有所帮助。

SET @processedtable := 'myprecioustablename';
SET @columnnames := (
    SELECT GROUP_CONCAT(COLUMN_NAME)
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = @processedtable);
SET @qrypartcount := REPLACE(@columnnames, ',','), COUNT(DISTINCT ');
SET @validquery := CONCAT("SELECT COUNT(DISTINCT ", @qrypartcount, ") FROM ", @processedtable);
PREPARE stmt FROM @validquery;
EXECUTE stmt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM