Mysql 如何使用select命令将3个不同表中的数据插入到一个表中
这是我的桌子Mysql 如何使用select命令将3个不同表中的数据插入到一个表中,mysql,sql,inner-join,aggregate-functions,sql-insert,Mysql,Sql,Inner Join,Aggregate Functions,Sql Insert,这是我的桌子 eleve表格 +----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+-------------+------+-----+---------+-------+ | idEleve | int(11) | NO | PRI
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 |
+---------+-----------+-------------+----------------+----------------+------------+
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)
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)
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)
注意
表格
+----------------+-------------+------+-----+---------+-------+
| 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 |
+---------+-----------+-------------+----------------+----------------+------------+
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)
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)
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)
affectationMatiere
表格
+----------------+-------------+------+-----+---------+-------+
| 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 |
+---------+-----------+-------------+----------------+----------------+------------+
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)
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)
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)
moyenne
表格
+----------------+-------------+------+-----+---------+-------+
| 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 |
+---------+-----------+-------------+----------------+----------------+------------+
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)
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)
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)
我想在moyenne
表中插入idEleve
、Annee
,以及每个学生的总体平均数
我的问题是:
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
表来生成预期的结果集
此外,我认为您需要聚合来收集所有codeMat
中每个(eleve
,annee
)的注释
我认为以下查询符合您的要求:
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
现在您构建了笛卡尔积,但实际上您希望使用条件进行联接。如果可能有一些学生还没有参加过任何课程,但也应该出现在结果中,那么您可能需要左连接。您还应该始终在
INSERT
语句中写下目标列
INSERT INTO moyenne
(ideleve,
annee,
moyenneglobaleeleve)
SELECT e.ideleve,
a.annee,
(notetrimester1
+ notetrimester2
+ notetrimester3)
/ 3
FROM eleve e
LEFT JOIN note n
ON n.ideleve = e.ideleve
LEFT JOIN affectationmatiere am
ON am.idaffectationorof = n.idaffectationprof;