mysql - MySQL 数列中的计数值
问题描述
我在 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”时。我将不胜感激任何帮助或建议。
解决方案
您可以使用 case when 或 if 和 one select
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
推荐阅读
- java - Jackson ObjectMapper to process JSON and non-JSON inputs
- python-2.7 - IOError: [Errno 22] invalid mode ('rb') or filename
- java - 如何通过 java(snakeYaml) 解析 yaml 文件
- r - Why geom segment does not work correctly?
- javascript - 从 React 中的 div 获取 prop 值
- bash - How can I use sed to change my target dir in this shell command line?
- java - Android studio Mail issue
- java - 如何仅从 url 获取 youtube 视频的标题?
- apache-spark - 使用包含具有不同模式的记录的 csv 设计 Spark 作业
- python - 我不知道如何显示此作业的第 3 步