future 的半新手主义:我需要进行两次选择并计算其中的项目数。这是一个我认为可行的坏例子 -
sum(
select count(*) as count1 from users where name = 'John'
union
select count(*) as count1 from users where name = 'Mary'
) as theCount
(正如我所说,这是一个糟糕的例子,因为我显然可以将其写为带有适当 WHERE 子句的单个选择。在我真正要做的事情中,我必须做的两件事是这样的:无法将它们作为单个选择来执行(或者,至少,我还没有找到一种方法来将它们作为单个选择来执行)。
无论如何,我认为我想要做的事情很明确:select-union-select 位返回一个包含两个选择的计数的列;那部分工作正常。我认为将它们包装在 SUM() 中会得到我想要的东西,但它引发了语法错误。正确的事情可能是微不足道的,但我只是看不到它。有什么想法吗?谢谢!
最佳答案
对于通用选择,您不一定可以用 where 来编写:
SELECT sum(count1) as totalcount FROM (
select count(*) as count1 from users where name = 'John'
union all
select count(*) as count1 from users where name = 'Mary'
) as theCount
关于mysql - 如何计算两次选择中的条目数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6865528/