【问题标题】:Count the number of columns populated in a row计算一行中填充的列数
【发布时间】:2021-06-23 11:41:29
【问题描述】:

我在sql中有一个这种风格的表:

id FirstName LastName Age
1 John Williams 25
2 Anne 23

我要统计每行填充的列数,如下图。

id FirstName LastName Age PopulatedColumns
1 John Williams 25 4
2 Anne 23 3

有谁知道如何制作一个在 sql 中计算的脚本?但是我不想指定列的名称,因为我有一个非常大的表,并且放置所有列的名称是不切实际的。有没有办法做到这一点?不写名字?

【问题讨论】:

  • 你要么必须使用名称,要么根据information_schema.columns创建sql语句并提交到动态sql
  • 但是当我做动态 sql ql 时,我只能得到列的名称,我不能做我需要的数学。可以举个例子吗?

标签: mysql sql qsqlquery


【解决方案1】:

在 MySQL 中,你可以使用算术:

select t.*,
       ( (id is not null) + (firstname is not null) + (lastname is not null) + (age is not null)
       ) as populated_columns
from t;

【讨论】:

  • 不输入列名可以吗?做一种贯穿所有列的循环吗?
  • @anonymous6 。 . .我不知道 MySQL 中有任何此类功能。
  • 只需在 Excel 中构建您的选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-09
  • 2017-08-18
  • 2022-11-10
  • 2013-09-11
  • 1970-01-01
  • 2015-06-19
相关资源
最近更新 更多