这是我的桌子
eleve
表
+----------------+-------------+------+-----+---------+-------+
| 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
表
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
表
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
表
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)
我想在表格中插入idEleve
,Annee
和每个学生的平均moyenne
分数。
这是我的查询:
INSERT INTO moyenne
SELECT E.idEleve,a.Annee, (note.NoteTrimester1 + note.NoteTrimester2 + note.NoteTrimester3)/3 AS NoteGlobal FROM eleve E,affectMatiere a, note;
但不幸的是,该请求返回了27个结果,而不是3个结果。突然间,我执行了没有插入到其中的请求,这就是我发现的结果
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)
查询的主要问题是您缺少表之间的连接条件:这导致3个表之间的笛卡尔积。请注意,就顾虑而言,您无需引入eleve
表即可生成预期结果集。
另外,我认为你需要聚集,聚集在一起的每个(笔记eleve
,annee
在所有)codeMat
秒。
我认为以下查询可以满足您的需求:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句