如何使用 select 命令将 3 个不同表中的数据插入到一个表中
Posted
技术标签:
【中文标题】如何使用 select 命令将 3 个不同表中的数据插入到一个表中【英文标题】:how to insert data from 3 different tables into one table using the select command 【发布时间】:2020-05-14 09:29:56 【问题描述】:这是我的桌子
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)
我想在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)
【问题讨论】:
【参考方案1】:您现在构建了一个笛卡尔积,但实际上您想要加入条件。如果可能有学生尚未参加任何课程但也应该出现在结果中,那么您可能需要左连接。您还应该始终在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;
【讨论】:
【参考方案2】:您的查询的主要问题是您缺少表之间的连接条件:这会导致 3 个表之间的笛卡尔积。请注意,就问题而言,您不需要引入eleve
表来生成预期的结果集。
另外,我认为您需要聚合以收集所有 codeMat
s 中每个(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
【讨论】:
以上是关于如何使用 select 命令将 3 个不同表中的数据插入到一个表中的主要内容,如果未能解决你的问题,请参考以下文章