我有一个名为Student_Info表的表,Subject_Info表和一个属于每个学生的个人表,其中包含有关该学生的每个主题参数的信息(如主题的名称,他在该主题中得到的标记等)。
Student_Info
+----+----------+
| Id | Name |
+----+----------+
| 1 | Sam |
| 2 | Taylor |
| 3 | Rick |
+----+----------+
Subject_Info
+---------------+
| Subject_Name |
+---------------+
| Physics |
| Chemistry |
| Mathematics |
+---------------+
1_Info
+---------------+-------+
| Subject_Name | Marks |
+---------------+-------|
| Physics | 60 |
| Chemistry | 40 |
| Mathematics | 80 |
+---------------+-------+
2_Info
+---------------+-------+
| Subject_Name | Marks |
+---------------+-------|
| Physics | 70 |
| Chemistry | 50 |
| Mathematics | 60 |
+---------------+-------+
3_Info
+---------------+-------+
| Subject_Name | Marks |
+---------------+-------|
| Physics | 70 |
| Chemistry | 70 |
| Mathematics | 70 |
+---------------+-------+
这里的表1_Info,2_Info,3_Info分别对应于Id = 1,Id = 2,Id = 3的学生。
我的问题是
我使用以下查询来获取表名;它工作正常。
SELECT CONCAT(Id, '_Info') FROM Student_Info;
我尝试使用像这样的单个查询来编写它
INSERT INTO (SELECT CONCAT(Id, '_Info') FROM Student_Info) VALUES ('Physics');
但是得到错误。
是否可以在单个查询中执行此操作?
如果是,那么我在哪里做错了?
答案 0 :(得分:0)
我认为将信息放在1表中会更容易,例如
Student
+----+--------+
| Id | Name |
+----+--------+
| 1 | Sam |
| 2 | Taylor |
| 3 | Rick |
+----+--------+
Subject
+----+-------------+
| Id | Subject |
+----+-------------+
| 1 | Physics |
| 2 | Chemistry |
| 3 | Mathematics |
+----+-------------+
Info
+------------+------------+-------+
| student_id | subject_id | Marks |
+------------+------------+-------+
| 1 | 1 | 60 |
| 1 | 2 | 40 |
| 1 | 3 | 80 |
| 2 | 1 | 70 |
| 2 | 2 | 50 |
| 2 | 3 | 60 |
| 3 | 1 | 70 |
| 3 | 2 | 70 |
| 3 | 3 | 70 |
+------------+------------+-------+
INSERT将是
INSERT INTO Student(Name) VALUES('Tom');
INSERT INTO Subject(Subject) VALUES('History');
INSERT INTO Info(4, 4, 80);
编辑:替代方案,这就是我认为的名称"第三范式(3NF)"
Student
+----+--------+
| Id | Name |
+----+--------+
| 1 | Sam |
| 2 | Taylor |
| 3 | Rick |
+----+--------+
Subject
+----+-------------+
| Id | Subject |
+----+-------------+
| 1 | Physics |
| 2 | Chemistry |
| 3 | Mathematics |
+----+-------------+
Marks
+----+------+
| Id | Mark |
+----+------+
| 1 | 60 |
| 2 | 40 |
| 3 | 80 |
| 4 | 70 |
| 5 | 50 |
+----+------+
Info
+------------+------------+---------+
| student_id | subject_id | mark_id |
+------------+------------+---------+
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 1 | 3 | 3 |
| 2 | 1 | 4 |
| 2 | 2 | 5 |
| 2 | 3 | 1 |
| 3 | 1 | 4 |
| 3 | 2 | 4 |
| 3 | 3 | 4 |
+------------+------------+---------+