gpt4 book ai didi

mysql - 计算多列中的不同值

转载 作者:行者123 更新时间:2023-11-29 04:17:00 34 4
gpt4 key购买 nike

我有一个数据库表

 projectNo| process | procLeader | procCheker  |  
---------+---------+------------+-------------+
16090001 | ANM | ari | barry |
16090001 | BLD | ben | ben |

我想计算多个列 procLeader 和 procChecker 中的不同变量,所以我想显示列名 total 3 as ben appears 2

到目前为止,我所做的是尝试将两列联合合并为一列,但现在不知道如何计算它。我的查询:

select distinct `procLeader`
as tot from process as tot where projectNo=16090016
union
select distinct `procChecker`
from process from process as tot where projectNo=16090016

或者也许有另一种方法来计算不同的变量?谢谢我希望最终结果是这样的

 total| 
-----+
3 |

最佳答案

在两列之间使用UNION去除重复,使用COUNT(*)得到计数结果。

SELECT COUNT(*) as Total
FROM
( SELECT `procLeader` FROM process where projectNo=16090016
UNION
SELECT `procCheker` FROM process where projectNo=16090016
) AS tmp

无需使用 DISTINCT 作为 UNION 运算符从结果集中删除重复项

关于mysql - 计算多列中的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39788205/

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