sql - How can I get count of nulls per database on a MySQL server? -
i count of nulls on columns , tables each database on mysql server. result table should like:
+---------------+------------+ | database_name | null_count | +---------------+------------+ | database1 | 0 | | database2 | 5643 | | database3 | 72 | +---------------+------------+
however, wasn't able beyond count of nulls single table:
select concat('select', group_concat(' count(*) - count(', column_name, ') ' separator ' + '), 'from ', max(table_schema), '.', max(table_name)) @sql information_schema.columns table_schema = 'accidents' , table_name = 'nesreca'; prepare stmt @sql; execute stmt; deallocate prepare stmt;
do have idea?
ps: able result matlab code. however, solution within mysql preferred.
i think on right track dynamic sql. because want entire database , not single table, structure of query like:
select table_schema, sum(cnt) (select "table_schema", count(*) cnt "table_name" "column_name" null union . . . ) t group table_schema;
you can construct as:
set group_concat_max_len=1500000; set @subsql = '(select "@table_schema" table_schema, count(*) cnt `@table_schema`.`@table_name` `@column_name` null)'; select @subsql := group_concat(replace(replace(replace(@subsql, '@table_schema', table_schema ), '@table_name', table_name ), '@column_name', column_name ) separator ' union ' ) information_schema.columns; set @sql = concat('select table_schema, sum(cnt) numnulls (', @subsql, ') t group table_schema'); prepare stmt @sql; execute stmt; deallocate prepare stmt;
i should note default length intermediate value group_concat()
small query. have adjust it. system variable group_concat_max_len
(see here).
Comments
Post a Comment