Search code examples
mysqlsqlinner-joinaggregate-functionssql-insert

how to insert data from 3 different tables into one table using the select command


here are my tables

eleve table

+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| idEleve        | int(11)     | NO   | PRI | NULL    |       |
| NomEleve       | varchar(30) | YES  |     | NULL    |       |
| PrenomEleve    | varchar(30) | YES  |     | NULL    |       |
| DateNaissEleve | date        | YES  |     | NULL    |       |
| LieuNaissEleve | varchar(30) | YES  |     | NULL    |       |
| codeClasse     | varchar(30) | YES  | MUL | NULL    |       |
+----------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> select *from eleve ;
+---------+-----------+-------------+----------------+----------------+------------+
| idEleve | NomEleve  | PrenomEleve | DateNaissEleve | LieuNaissEleve | codeClasse |
+---------+-----------+-------------+----------------+----------------+------------+
|       1 | brahim    | elmoctar    | 1996-08-19     | teyaret        | CP1        |
|       2 | mohamed   | elmoctar    | 2000-02-01     | teyaret        | CP2        |
|       3 | fatimetou | elmoctar    | 1995-05-19     | teyaret        | CP3        |
+---------+-----------+-------------+----------------+----------------+------------+

note table

mysql> desc note ;
+-------------------+---------+------+-----+---------+-------+
| Field             | Type    | Null | Key | Default | Extra |
+-------------------+---------+------+-----+---------+-------+
| idAffectationProf | int(11) | YES  | MUL | NULL    |       |
| idEleve           | int(11) | YES  | MUL | NULL    |       |
| NoteTrimester1    | double  | YES  |     | NULL    |       |
| NoteTrimester2    | double  | YES  |     | NULL    |       |
| NoteTrimester3    | double  | YES  |     | NULL    |       |
+-------------------+---------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> select * from note ;
+-------------------+---------+----------------+----------------+----------------+
| idAffectationProf | idEleve | NoteTrimester1 | NoteTrimester2 | NoteTrimester3 |
+-------------------+---------+----------------+----------------+----------------+
|                 1 |       1 |          13.24 |          12.45 |          10.54 |
|                 2 |       1 |          10.24 |          17.45 |          18.54 |
|                 3 |       1 |          15.24 |          12.45 |          13.54 |
+-------------------+---------+----------------+----------------+----------------+
3 rows in set (0.00 sec)

affectationMatiere table

mysql> desc affectationMatiere ;
+-------------------+-------------+------+-----+---------+-------+
| Field             | Type        | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+-------+
| idAffectationProf | int(11)     | NO   | PRI | NULL    |       |
| codeClasse        | varchar(30) | YES  | MUL | NULL    |       |
| idProf            | int(11)     | YES  | MUL | NULL    |       |
| codeMat           | varchar(30) | YES  | MUL | NULL    |       |
| Annee             | date        | YES  |     | NULL    |       |
+-------------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> select *from affectationMatiere ;
+-------------------+------------+--------+---------+------------+
| idAffectationProf | codeClasse | idProf | codeMat | Annee      |
+-------------------+------------+--------+---------+------------+
|                 1 | CP1        |      1 | Math    | 2020-01-01 |
|                 2 | CP2        |      2 | PC      | 2020-02-02 |
|                 3 | CP3        |      1 | SN      | 2020-03-03 |
+-------------------+------------+--------+---------+------------+
3 rows in set (0.00 sec)

moyenne table

mysql> desc moyenne ;
+---------------------+---------+------+-----+---------+-------+
| Field               | Type    | Null | Key | Default | Extra |
+---------------------+---------+------+-----+---------+-------+
| idEleve             | int(11) | YES  | MUL | NULL    |       |
| Annee               | date    | NO   | PRI | NULL    |       |
| moyenneGlobaleEleve | double  | YES  |     | NULL    |       |
+---------------------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

I want to insert idEleve, Annee, and the general average of each student in the moyenne table.

Here is my query:

INSERT INTO moyenne 
    SELECT E.idEleve,a.Annee, (note.NoteTrimester1 + note.NoteTrimester2 + note.NoteTrimester3)/3 AS NoteGlobal FROM eleve E,affectMatiere a, note;

but unfortunately the request returns 27 result instead of 3. suddenly I executed the request without the insert into and here is what I found

mysql> SELECT E.idEleve,a.Annee, (note.NoteTrimester1 + note.NoteTrimester2 + note.NoteTrimester3)/3 AS NoteGlobal FROM eleve E,affectationMatiere a, note;
+---------+------------+--------------------+
| idEleve | Annee      | NoteGlobal         |
+---------+------------+--------------------+
|       1 | 2020-01-01 | 12.076666666666666 |
|       2 | 2020-01-01 | 12.076666666666666 |
|       3 | 2020-01-01 | 12.076666666666666 |
|       1 | 2020-02-02 | 12.076666666666666 |
|       2 | 2020-02-02 | 12.076666666666666 |
|       3 | 2020-02-02 | 12.076666666666666 |
|       1 | 2020-03-03 | 12.076666666666666 |
|       2 | 2020-03-03 | 12.076666666666666 |
|       3 | 2020-03-03 | 12.076666666666666 |
|       1 | 2020-01-01 | 15.409999999999998 |
|       2 | 2020-01-01 | 15.409999999999998 |
|       3 | 2020-01-01 | 15.409999999999998 |
|       1 | 2020-02-02 | 15.409999999999998 |
|       2 | 2020-02-02 | 15.409999999999998 |
|       3 | 2020-02-02 | 15.409999999999998 |
|       1 | 2020-03-03 | 15.409999999999998 |
|       2 | 2020-03-03 | 15.409999999999998 |
|       3 | 2020-03-03 | 15.409999999999998 |
|       1 | 2020-01-01 | 13.743333333333332 |
|       2 | 2020-01-01 | 13.743333333333332 |
|       3 | 2020-01-01 | 13.743333333333332 |
|       1 | 2020-02-02 | 13.743333333333332 |
|       2 | 2020-02-02 | 13.743333333333332 |
|       3 | 2020-02-02 | 13.743333333333332 |
|       1 | 2020-03-03 | 13.743333333333332 |
|       2 | 2020-03-03 | 13.743333333333332 |
|       3 | 2020-03-03 | 13.743333333333332 |
+---------+------------+--------------------+
27 rows in set (0.00 sec)

Solution

  • The main issue with your query is that you are missing join conditions between the tables: this results in a cartesian product between the 3 tables. Note that, as far as concerns, you don't need to bring in the eleve table to generate the expected resultset.

    Also, I think that you need aggregation to gather together notes of each (eleve, annee) across all codeMats.

    I think that the following query does what you want:

    insert into moyenne 
    select 
        n.idEleve,
        a.Annee, 
        avg(n.NoteTrimester1 + n.NoteTrimester2 + n.NoteTrimester3)/3 
    from 
        note n
        inner join affectMatiere a on a.idAffectationProf = n.idAffectationProf
    group by 
        n.idEleve,
        a.Annee