gpt4 book ai didi

mysql - 如何将非 NULL 列计数到同一个表中?

转载 作者:行者123 更新时间:2023-11-29 06:58:58 25 4
gpt4 key购买 nike

如何对不为空或零的表列进行计数,并将结果插入到 Mysql 中的“计数”列中,如下表所示?

 column1 | column2 | column3 | column4 | Count
3 2 4 2 4
2 2 3 0 3
3 0 5 2 3
0 0 2 3 2
0 0 6 2 2

最佳答案

一种方法是简单地使用 case 语句:

编辑:

要更新 table_name 中的 cnt 列,您可以使用:

update table_name 
set cnt = (case when column1 is null or column1 = 0 then 0 else 1 end +
case when column2 is null or column2 = 0 then 0 else 1 end +
case when column3 is null or column3 = 0 then 0 else 1 end +
case when column4 is null or column4 = 0 then 0 else 1 end)

关于mysql - 如何将非 NULL 列计数到同一个表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44323191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com