回答
如果你想要这个由MySQL,没有列举所有的列专门做取看看这个解决方案。
在这种方法中,您不必通过硬编码来维护数据库列的数量。如果你的表模式将被修改,这个方法将会工作,并且不需要修改代码。
SET @db = 'testing'; -- database
SET @tb = 'fuzzysearch'; -- table
SET @x = ''; -- will hold the column names with ASCII method applied to retrieve the number of the first char
SET @numcolumns = 0; -- will hold the number of columns in the table
-- figure out how many columns we have
SELECT count(*) into @numcolumns FROM information_schema.columns where [email protected] and [email protected];
-- we have to prepare some query from all columns of the table
SELECT group_concat(CONCAT('ASCII(',column_name,')') SEPARATOR ",") into @x from information_schema.columns where [email protected] and [email protected];
-- after this query we have a variable separated with comma like
-- ASCII(col1),ASCII(col2),ASCII(col3)
-- we now generate a query to concat the columns using comma as separator (null values are omitted from concat)
-- then figgure out how many times the comma is in that substring (this is done by using length(value)-length(replace(value,',',''))
-- the number returned is how many non null columns we have in that column
-- then we deduct the number from the known number of columns, calculated previously
-- the +1 is added because there is no comma for single value
SET @s = CONCAT('SELECT @numcolumns - (length(CONCAT_WS(\',\',', @x, '))-length(replace(CONCAT_WS(\',\',', @x, '),\',\',\'\')) + 1) FROM ',@db,'.',@tb,';');
PREPARE stmt FROM @s;
EXECUTE stmt;
-- after this execution we have returned for each row the number of null columns
-- I will leave to you to add a sum() group call if you want to find the null values for the whole table
DEALLOCATE PREPARE stmt;
ASCII码是用来避免阅读,串联非常长柱没出息,也ASCII让我们安全的值,其中第一个字符是一个逗号(,)。
由于您正在使用报告,因此如果您将方法放在某个方法中,您可能会发现这有助于为每个表重用。
我试着让尽可能多的评论。
让我们在片分割上述紧凑的方式(逆向):
我想最终拥有这样
SELECT totalcolumns - notnullcolumns from table; -- to return null columns for each row
查询虽然第一个是容易通过运行到calcule:
SELECT count(*) FROM information_schema.columns where [email protected] and [email protected];
第二个notnullcolumns是有点痛苦。 一块在MySQL中可用的功能检查后,我们检测到CONCAT_WS不CONCAT空值
所以运行这样的查询:
SELECT CONCAT_WS(",","First name",NULL,"Last Name");
returns: 'First name,Last Name'
这是好事,我们就摆脱了空的枚举值。 但是,我们如何获得实际连接的列数?
那么,这是棘手。我们必须计算逗号+1来获得实际连接的列。
对于这一招,我们使用下面的SQL注释
select length(value)-length(replace(value,',','')) +1 from table
好了,所以我们现在有串联的列数。
但是更难的部分是未来。
我们必须枚举CONCAT_WS()的所有值。
我们需要有这样的事情:
SELECT CONCAT_WS(",",col1,col2,col3,col4,col5);
这是我们必须采取使用预处理语句的,因为我们必须从未知列准备动态SQL查询。我们不知道我们的桌子上有多少列。
因此,为此我们使用information_schema列表中的数据。我们需要传递表名,但也要传递数据库名,因为我们可能在不同的数据库中使用相同的表名。
我们需要返回COL1,COL2,COL3,COL4,COL5我们在CONCAT_WS “串”
因此,对于这一点,我们运行一个查询
SELECT group_concat(column_name SEPARATOR ",") into @x from information_schema.columns where [email protected] and [email protected];
还有一点要提的查询。当我们使用length()和replace()方法来找出连接了多少列时,我们必须确保这些值之间没有逗号。但是请注意,我们在数据库单元中可以有很长的值。对于这个技巧,我们使用方法ASCII('value'),它将返回第一个char的ASCII字符,它不能是逗号,并且对于空列将返回null。
这就是说,我们可以在上述综合解决方案中压缩所有这些。
喜欢的东西
select id
, sum (case when col1 is null then 1 else 0 end case) col1
, sum (case when col2 is null then 1 else 0 end case) col2
, sum (case when col3 is null then 1 else 0 end case) col3
from contacts
group by id
你应该这样不仅使用SQL,但是真正做的是在您的处置语言:
获取每个表的元数据 - 无论是使用
DESCRIBE table
,或使用db访问技术中的内置元数据功能在每个柱的循环中创建以下类型的查询ñ。 (伪代码)
int nulls = 0; for (String colmnName : columNames) { query = "SELECT COUNT(*) FROM tableName WHERE " + columnName + " IS NULL"; Result result = executeQuery(query); nulls += result.size(); }
像这样的东西(替代COL_COUNT如适用):
select count(*) * COL_COUNT - count(col1) - count(col2) - ... - count(col_n) from table;
这涉及知道该表的列数,在大表上它不具有生产力。检查我的解决方案 – Pentium10 2010-02-19 13:00:03
- 1. 如何计算Access 2010表中所有列中的所有NULL值?
- 2. 用NULL值计算
- 3. 计算anylogic中流量的所有值
- 4. 计算表中所有行的TotalAmount
- 5. 如何计算MySQL中所有行中的所有字符?
- 6. 如何计算PostgreSQL中表中所有行的日期差异?
- 7. SparkR。如何计算Spark DataFrame中所有列的不同值?
- 8. 如何计算Python中所有列的异常值?
- 9. 如何计算UITableView单元格中的所有值?
- 10. JS如何计算表中的行值?
- 11. 如何计算表中的特定值
- 12. 如何计算列表中所有可能的分区?
- 13. 如何获取计算机中所有打印机的列表
- 14. 获取从左表中的所有记录与计算列值
- 15. 如何计算我所有的输入
- 16. 如何计算XElement的所有标签?
- 17. 如何计算所有的MySQL导致
- 18. 计算utf8的所有可能值
- 19. 如何在计时器中计算所计算的时间?
- 20. 计算SUM即使他们中的一些有NULL值
- 21. 如何在Oracle/PLSQL中只计算NULL值?
- 22. 如何在BigQuery中选择数据的列有所有NULL值
- 23. 如何计算所有列的平均值?
- 24. 如何计算所有行的平均值
- 25. SQL如何计算所有行的最大值
- 26. 如何使用类方法计算阵列中所有值的平均值
- 27. 有效的中值计算
- 28. 如何计算在所有行表表发现
- 29. 如何计算MultiMap的列表值?
- 30. 计算Excel数据透视表中所有值的过滤值的百分比
有一种方法可以在MySQL中专门用SQL来实现。代码并不简单。看看我发布的解决方案。 – Pentium10 2010-02-19 12:48:43