gpt4 book ai didi

mysql - 对多个 COUNT 求和的 UNION 语法

转载 作者:行者123 更新时间:2023-12-01 00:53:56 27 4
gpt4 key购买 nike

这些查询单独工作。如何编写 UNION 以对所有结果求和?

SELECT SUM(players) FROM `tournament_players` FROM (
SELECT COUNT(*) as `players` FROM `tournament_players` WHERE `foursome_1_p1_name` IS NOT NULL AND `tournament` BETWEEN 13 AND 17 AND flight = '8 AM'
UNION
SELECT COUNT(*) as `players` FROM `tournament_players` WHERE `foursome_1_p2_name` IS NOT NULL AND `tournament` BETWEEN 13 AND 17 AND flight = '8 AM'
UNION
SELECT COUNT(*) as `players` FROM `tournament_players` WHERE `foursome_1_p3_name` IS NOT NULL AND `tournament` BETWEEN 13 AND 17 AND flight = '8 AM'
UNION
SELECT COUNT(*) as `players` FROM `tournament_players` WHERE `foursome_1_p4_name` IS NOT NULL AND `tournament` BETWEEN 13 AND 17 AND flight = '8 AM'
)

最佳答案

您走在正确的轨道上,但有一个额外的 FROM 关键字。试试这个:

SELECT SUM(players) FROM (
SELECT COUNT(*) as `players` FROM `tournament_players` WHERE `foursome_1_p1_name` IS NOT NULL AND `tournament` BETWEEN 13 AND 17 AND flight = '8 AM'
UNION
SELECT COUNT(*) as `players` FROM `tournament_players` WHERE `foursome_1_p2_name` IS NOT NULL AND `tournament` BETWEEN 13 AND 17 AND flight = '8 AM'
UNION
SELECT COUNT(*) as `players` FROM `tournament_players` WHERE `foursome_1_p3_name` IS NOT NULL AND `tournament` BETWEEN 13 AND 17 AND flight = '8 AM'
UNION
SELECT COUNT(*) as `players` FROM `tournament_players` WHERE `foursome_1_p4_name` IS NOT NULL AND `tournament` BETWEEN 13 AND 17 AND flight = '8 AM'
) `tournament players`

关于mysql - 对多个 COUNT 求和的 UNION 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12824040/

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