我尝试在 mysql 中进行更多高级查询,但查询出错,我尝试计数和分组。这是我的查询:
"select department,
(select count(id) from mcu group by dept) as tot1,
(select count(status_mcu) from mcu where status_mcu='done' group by dept) as tot2,
(tot1 - sum(select count(status_mcu) where status_mcu='done') as tot3,
FROM mcu"
你能帮我吗 ?我的查询有什么问题?我在其他线程中进行了一些搜索,这就是我得到的。尝试像这样查询:
"select department,
select count(id),
sum(case when status_mcu='done' then 1 end)
...
"
也没有工作。任何帮助表示赞赏。
我的 sql 版本是 5.5.25 – casthrotopes
如果没有看到返回的确切错误消息,我不能 100% 确定,但有两个明显的错误:
Department
还是Dept
?这似乎并不清楚。tot1
除非它首先在 CTE 或子选择(派生表)中派生,否则您无法引用。sum(select count(status_mcu) where status_mcu='done')
没有FROM
子句。此外,我不确定需要/需要聚合高级聚合的目的是什么。从这里开始,看看它是否返回所需的结果(如果没有,您需要澄清您的问题以提供正确的逻辑):