This query tells you each note, and gives you a row name for each one. Not bad, right?
SELECT nota, COUNT(*) total
FROM (
SELECT nota1 as nota FROM estudiantes_20171010
UNION ALL
SELECT nota2 as nota FROM estudiantes_20171010
UNION ALL
SELECT nota3 as nota FROM estudiantes_20171010
UNION ALL
SELECT nota4 as nota FROM estudiantes_20171010
UNION ALL
SELECT nota5 as nota FROM estudiantes_20171010
) tbl_tmp
GROUP BY nota;
Although I would improve the design of the database, so we avoid headaches ... :) A simple table that saves id_estudiante
, id_asignatura
and nota
would give an interesting respite to the table estudiantes
, would allow you to tell what you want, even if there were thousands of types of notes, etc ...
I hope it's useful for you.
Code
VIEW DEMO
CREATE TABLE IF NOT EXISTS estudiantes_20171010
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
nombre VARCHAR(20), nota1 CHAR(1), nota2 CHAR(1),nota3 CHAR(1),nota4 CHAR(1),nota5 CHAR(1)
)ENGINE=INNODB;
INSERT INTO estudiantes_20171010 (nombre,nota1,nota2,nota3,nota4,nota5)
VALUES
('Estudiante1','E','S','S','A','S'),
('Estudiante2','A','S','A','E','S'),
('Estudiante3','S','E','S','S','S')
;
-- Datos de prueba
SELECT *
FROM estudiantes_20171010
;
-- Contando notas
SELECT nota, COUNT(*) total
FROM (
SELECT nota1 as nota FROM estudiantes_20171010
UNION ALL
SELECT nota2 as nota FROM estudiantes_20171010
UNION ALL
SELECT nota3 as nota FROM estudiantes_20171010
UNION ALL
SELECT nota4 as nota FROM estudiantes_20171010
UNION ALL
SELECT nota5 as nota FROM estudiantes_20171010
) tbl_tmp
GROUP BY nota;
- Test data
id nombre nota1 nota2 nota3 nota4 nota5
1 Estudiante1 E S S A S
2 Estudiante2 A S A E S
3 Estudiante3 S E S S S
- Counting
nota total
A 3
E 3
S 9