gpt4 book ai didi

MySQL统计不包含特定值的字段数并将其存储在另一个字段中

转载 作者:行者123 更新时间:2023-11-30 00:39:46 25 4
gpt4 key购买 nike

我有两个 MySQL 表。

1) wfGroups 表包含字段

wfs1, wfs2, wfs3, ..., wfs13, wfs14, wfs15 

其中包含 text strings0are blank if there is no value in the field

2) wf 表包含一个标题为

的字段
totalSteps 

我想包含一个 count of the number of fields from the wfGroups table that don't contain the "0" or blank values

one wfGroups 表行 for each wf table row两个表中的每一行都有一个 userId and wfId有助于确定哪些数据属于一起的字段。我有大约一千行需要计算totalSteps 值。

此查询只需运行一次,之后字段将在发生更改时自行更新,因此速度不是问题。

我是 SQL 新手,我完全不知道从哪里开始创建这个查询。我发现了一些可能有用的代码,但我不知道如何将它们组合在一起并使其循环遍历沿途存储计算值的每一行。

最佳答案

使用连接更新:

update wf w
join wfgroups g on g.userId = w.userId and g.wfId = w.wfId
set w.totalSteps = wfs1 not in ("0", "") + wfs2 not in ("0", "") + wfs3 not in ("0", "")
+ wfs4 not in ("0", "") + wfs5 not in ("0", "") + wfs6 not in ("0", "")
+ wfs7 not in ("0", "") + wfs8 not in ("0", "") + wfs9 not in ("0", "")
+ wfs10 not in ("0", "") + wfs11 not in ("0", "") + wfs12 not in ("0", "")
+ wfs13 not in ("0", "") + wfs14 not in ("0", "") + wfs15 not in ("0", "");

这是有效的,因为在mysql中,true1false0,所以通过添加条件,你获取它们为真的次数。

关于MySQL统计不包含特定值的字段数并将其存储在另一个字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871560/

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