group result of statement mysql-workbench

1

I have this sentence.

select nombrem,idparcial,calificacion from calificacion
inner join parcial on parcial.idparcial=calificacion.fkparcial
inner join materia on materia.idmateria=calificacion.fkmateria;

and it gives me this result.

nombrem|idparcial|calificacion|
ingles |    1    |   10
ingles |    2    |   10
ingles |    3    |   10
POO    |    1    |   7
POO    |    2    |   7
POO    |    3    |   7

What I want is that they are grouped by matter more or less something like that

nombrem|idparcial|calificacion|idparcial|calificacion|idparcial|calificacion|
ingles |    1    |   10   |    2    |   10       |    3    |   10
POO    |    1    |   10   |    2    |   10       |    3    |   10

I try to put group by and with a multiple inner join but it does not fit me in any way.

create table cuatrimestre(
idcuatri int not null auto_increment,
primary key (idcuatri));

create table materia (
idmateria int not null auto_increment,
nombrem varchar(150) not null,
fkcuatri int not null,
primary key (idmateria),
foreign key (fkcuatri) references cuatrimestre (idcuatri));

create table parcial(
idparcial int not null auto_increment,
primary key (idparcial));

create table calificacion (
idcalificacion int not null auto_increment,
calificacion double not null,
fkparcial int not null,
fkmateria int not null,
primary key (idcalificacion),
foreign key (fkparcial) references parcial(idparcial),
foreign key (fkmateria) references materia (idmateria));

This is my database, will it be that I have poorly structured? and in this case, how would the database be correctly related Thank you in advance.

    
asked by Diego Montiel 31.07.2018 в 03:45
source

1 answer

1

As I understand it, the subjects have several partials and these are assigned a grade. Certainly you have poorly structured database, because the partial is who should have the foreign key of the subject, not the qualification. However, the way you want to get the information is still a little weird. So, if you really need that structure, I think it could be more efficient to use a database like Mongodb , because it will allow you to structure with greater freedom your data.

However, to make the query you want, you can use the following sentence:

select m.nombrem, c.p1, c.c1, c.p2, c.c2, c.p3, c.c3 from 
(select c1.fkmateria, 
c1.fkparcial as 'p1', c1.calificacion as 'c1',
c2.fkparcial as 'p2', c2.calificacion as 'c2',
c3.fkparcial as 'p3', c3.calificacion as 'c3'
from calificacion c1, calificacion c2, calificacion c3
where c1.idcalificacion != c2.idcalificacion 
and c1.idcalificacion != c3.idcalificacion
and c2.idcalificacion != c3.idcalificacion
and c1.fkmateria = c2.fkmateria
and c1.fkmateria = c3.fkmateria
group by c1.fkmateria) c 
INNER JOIN materia m ON c.fkmateria = m.idmateria;

You can try it here: SQL Fiddle .

What I am doing is multiplying the chart of qualifications n times, n being the number of partials, and verifying that the three qualifications are different but corresponds to the same subject and then I group them by subject. In this way, I get the structure you want in the result and finally you make a join with the table materials to replace the id of the subject with the name of it.

    
answered by 31.07.2018 / 04:41
source