不漂亮,但这是你不想违反第一范式并拥有多值列的一个原因......
select 'a' as grade, count(*) as occurrences
from student
where grade like '%a%'
union all
select 'b' as grade, count(*) as occurrences
from student
where grade like '%b%'
union all
select 'c' as grade, count(*) as occurrences
from student
where grade like '%c%'
union all
select 'd' as grade, count(*) as occurrences
from student
where grade like '%d%'
在这里查看它的实际应用。
或者,如果您有 grades
Chris K 提出的表格,您可以执行以下操作:
select g.grade, count(s.student_name) as occurances
from
grades g
left join student s
on concat(',', s.grade, ',') like concat('%,', g.grade, ',%')
group by g.grade
在这里查看它的实际应用。