Get several data in a single mysql query

2

I have a mysql database of the index of a course

(id,tipo,titulo,completado)

I keep the different types that make up the index (classes, exercises and exams). I need to know how many exercises, classes and exams the course has to do so:

SELECT COUNT() FROM indice_curso WHERE tipo=examen;
SELECT COUNT() FROM indice_curso WHERE tipo=clase;
SELECT COUNT() FROM indice_curso WHERE tipo=ejercicio;

But there are 3 queries, is it possible to do it in 1 query ?. Thank you very much !!

    
asked by Patricio 21.04.2017 в 20:40
source

3 answers

1

After editing the question, I would recommend using the operation of UNION sets:

SELECT COUNT() FROM indice_curso WHERE tipo=examen
UNION
SELECT COUNT() FROM indice_curso WHERE tipo=clase
UNION
SELECT COUNT() FROM indice_curso WHERE tipo=ejercicio

I think that's what you're looking for

    
answered by 21.04.2017 в 21:00
1

At least in SQL Server it can be solved like this, I suppose that in mysql you will be able to do something similar:

SELECT SUM(CASE WHEN tipo=examen THEN 1 ELSE 0 END),
       SUM(CASE WHEN tipo=clase THEN 1 ELSE 0 END),
       SUM(CASE WHEN tipo=ejercicio THEN 1 ELSE 0 END)
       FROM indice_curso
    
answered by 24.04.2017 в 16:09
0

You could use a statement similar to this:

    SELECT COUNT() FROM indice_curso WHERE (tipo = 'examen' OR tipo = 'clase' OR tipo = 'ejercicio');

I think it can help you.

    
answered by 21.08.2017 в 09:40