计算表中每一列的非空值

发布于 2024-12-02 00:31:33 字数 448 浏览 6 评论 0原文

我正在寻找数据迁移项目中丢失的数据,这份报告将对我有很大帮助。

给定一个 MySQL 表,我想计算该表每一行中的所有空(NULL 或 '')值。输出将是列名称列表以及每列的空行或非空行计数。我会手动将这些数据与源表进行比较 - 手动是因为我预计很少有计数能够完全匹配,并且源表和导入表之间的列名称完全不同。

我有大约 30 个表需要检查,其中一些有 100 列。我可以从 PC 直接访问 MySQL,但无法在带有数据库的服务器上运行任何脚本。

TableA 的示例

Col1 Col2 Col3
'XX' NULL 'XX'
'XX' NULL ''
'XX' 'XX' 'XX'
'XX' ''   'XX'

我想要的报告是(对于带有 '' 的非空计数计为空):

Col1: 4
Col2: 1
Col3: 3

I am looking for missing data in a data migration project, and this report will help me immensely.

Given a MySQL table, I would like to count all the empty (NULL or '') values in each row of that table. The output would be a list of column names and a count of empty or non-empty rows for each column. This data is something I would manually compare to the source tables - manually because I expect few counts to match up exactly and column names are completely different between the source and imported tables.

I have about 30 tables to check, a few with 100 columns. I have direct MySQL access from my PC, but no access to run any scripts on the server with the database.

Example for TableA

Col1 Col2 Col3
'XX' NULL 'XX'
'XX' NULL ''
'XX' 'XX' 'XX'
'XX' ''   'XX'

The report I would want is (for non-empty counts with '' counted as empty):

Col1: 4
Col2: 1
Col3: 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尤怨 2024-12-09 00:31:33

COUNT 也会对空字符串进行计数,因此您的查询应如下所示:

SELECT COUNT( NULLIF( col1, '' ) ), COUNT( NULLIF( col2, '' ) ), ...

COUNT counts empty strings too, so your query should look like this:

SELECT COUNT( NULLIF( col1, '' ) ), COUNT( NULLIF( col2, '' ) ), ...
郁金香雨 2024-12-09 00:31:33

您可以对每个表使用以下查询

SELECT COUNT(*), COUNT(col1) as col1, COUNT(col2) as col2
FROM TABLE1

要获取特定表的所有列,您应该运行查询

 select column_name from information_schema.columns where TABLE_NAME='TABLE1';

此查询的结果,您可以将其用于自动生成查询,如第一个查询。

You can use the following query for each table

SELECT COUNT(*), COUNT(col1) as col1, COUNT(col2) as col2
FROM TABLE1

For getting all columns for a specific table you should run query

 select column_name from information_schema.columns where TABLE_NAME='TABLE1';

Results of this query you can use for auto generation queries like the first one.

甜点 2024-12-09 00:31:33

仅计算具有值的行(跳过 Null/空行)!!!

SELECT COUNT( NULLIF( Column_Name, '' ) ) from Table_name

Count rows only which has values (Skipping Null/Empty rows)!!!

SELECT COUNT( NULLIF( Column_Name, '' ) ) from Table_name
時窥 2024-12-09 00:31:33

这对我有用

SELECT count( NULLIF( doctor_id, '' )) as doctor_count,count( NULLIF( chemist_id, '' )) as chemistcount from table_name WHERE employee_id="20";

this worked for me

SELECT count( NULLIF( doctor_id, '' )) as doctor_count,count( NULLIF( chemist_id, '' )) as chemistcount from table_name WHERE employee_id="20";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文