我在MySQL中有一张包含学生成绩的表格。我想发出一个单个SQL查询,该查询将从所有列(例如
adNumber |Eng | Kis| Math| Phys| chem
1254 | A | B | B | A | D
2147 | B | A | A | A | C
3241 | D | A | C | A | E
我想得到类似以下的内容:
Eng| kIS| Math | Phys | chem
1 | 2 | 1 | 3 | 0
当我发出要从所有列中计数“ A”的语句时。我将不胜感激。
答案 0 :(得分:2)
您可以在以下情况下使用一种情况:或if和一个选择
select sum(case when Eng = 'A' then 1 else 0 end) as Eng
, sum(case when Kis = 'A' then 1 else 0 end) as Kis
, sum(case when Math = 'A' then 1 else 0 end) as Math
, sum(case when Phys = 'A' then 1 else 0 end) as Phys
, sum(case when Chem = 'A' then 1 else 0 end) as Chem
from Grades
答案 1 :(得分:0)
您可以通过子查询来实现
select
(Select count(*) from Grades where Eng = 'A') as Eng,
(Select count(*) from Grades where Kis = 'A') as Kis,
(Select count(*) from Grades where Math = 'A') as Math,
(Select count(*) from Grades where Phys = 'A') as Phys,
(Select count(*) from Grades where Chem = 'A') as Chem
答案 2 :(得分:0)
尝试此查询:
select sum(case when Eng = 'A' then 1 else 0 end) Eng,
sum(case when Kis = 'A' then 1 else 0 end) Kis,
sum(case when Math = 'A' then 1 else 0 end) Math,
sum(case when Phys = 'A' then 1 else 0 end) Phys,
sum(case when chem = 'A' then 1 else 0 end) chem
from Grades