我需要在一个表中插入多个表和多行。
让我们说有3张桌子
1:主题表
+----+-------------+-------+
| ID | Subject | Class |
+----+-------------+-------+
| 1 | English | 1 |
| 2 | Mathematics | 1 |
| 3 | Sciences | 1 |
| 4 | English | 2 |
| 5 | Mathematics | 2 |
+----+-------------+-------+
2:月份表
+----+-------+
| ID | Month |
+----+-------+
| 1 | Oct |
| 2 | Nov |
| 3 | Desc |
+----+-------+
2:用户表
+----+------+---------+
| ID | Name | ClassID |
+----+------+---------+
| 1 | XerK | 1 |
| 2 | cdya | 1 |
| 3 | nija | 2 |
| 4 | Sara | 2 |
| 5 | Noda | 2 |
+----+------+---------+
现在我需要在此表中插入所有这些
结果表
+----+---------+----------------+---------+-------+-------+-------+
| ID | userID | subjectID | MonthID | Exam1 | Exam2 | Exam3 |
+----+---------+----------------+---------+-------+-------+-------+
| 1 | 1(xerk) | 1(English) | 1(oct) | NULL | NULL | NULL |
| 2 | 1(xerk) | 2(Mathematics) | 1(oct) | NULL | NULL | NULL |
| 3 | 1(xerk) | 3(Sciences) | 1(oct) | NULL | NULL | NULL |
| 4 | 1(xerk) | 1(English) | 2(nov) | NULL | NULL | NULL |
| 5 | 1(xerk) | 2(Mathematics) | 2(nov) | NULL | NULL | NULL |
| 6 | 1(xerk) | 3(Sciences) | 2(nov) | NULL | NULL | NULL |
| 7 | 1(xerk) | 1(English) | 3(desc) | NULL | NULL | NULL |
| 8 | 1(xerk) | 2(Mathematics) | 3(desc) | NULL | NULL | NULL |
| 9 | 4(Sara) | 3(Sciences) | 3(desc) | NULL | NULL | NULL |
| 10 | 4(Sara) | 4(English) | 1(oct) | NULL | NULL | NULL |
| 11 | 4(Sara) | 5(Mathematics) | 1(oct) | NULL | NULL | NULL |
| 12 | 4(Sara) | 4(English) | 2(nov) | NULL | NULL | NULL |
| 13 | 4(Sara) | 5(Mathematics) | 2(nov) | NULL | NULL | NULL |
| 14 | 4(Sara) | 4(English) | 3(desc) | NULL | NULL | NULL |
| 15 | 4(Sara) | 5(Mathematics) | 3(desc) | NULL | NULL | NULL |
+----+---------+----------------+---------+-------+-------+-------+
如何在页面PHP中通过一个按钮插入所有这些? 需要插入每个用户都有subjectID和MonthID。
解决方案
INSERT INTO results
(Col1, Col2, Col3, Col4, Col5, Col6)
SELECT
users.Col1, subjects.Col2, months.Col3, 0, 0, 0
FROM
(SELECT Col1 FROM users) as users
CROSS JOIN
(SELECT Col2 FROM subjects) as subjects
CROSS JOIN
(SELECT Col3 FROM months) as months
我找到了答案,但我需要在结果表中插入新的成员数据而不重复旧成员。