我有 2 个 mysql 数据库。(1) authorID、authorFirst、authorLast。(2) courseID, courseName, courseAuthor1, courseAuthor2, courseAuthor3, courseAuthor4。如何将作者数据库中的 2 列连接到课程数据库中的一个或全部 4 个 courseAuthor 列中?
作者
authorID | authorFirst | authorLast
1 | firstA | lastA
2 | firstB | lastB
3 | firstC | lastC
4 | firstD | lastC
培训班
courseID | courseName | courseAuthor1 | courseAuthor2 | courseAuthor3 | courseAuthor4
1 | course1 | 2 | 3 | NULL | NULL
2 | course 2 | 1 | 4 | 3 | NULL
3 | course 33 | 4 | 1 | 2 | NULL
4 | course 4 | 3 | NULL | NULL | NULL
我希望结果看起来像这样:
courseID | courseName | courseAuthor1 | courseAuthor2 | courseAuthor3 | courseAuthor4
1 | course1 | firstB LastB | firstC LastC | NULL | NULL
2 | course 2 | firstA LastA | firstD LastD | firstC LastC | NULL
3 | course 33 | firstD LastD | firstA LastA | firstB LastB | NULL
4 | course 4 | firstC LastC | NULL | NULL | NULL
我什至不知道从哪里开始。这是我的代码:
SELECT * FROM courses 在 authors.authorID 中加入作者(courses.courseAuthor1、courses.courseAuthor2、courses.courseAuthor3、courses.courseAuthor4)
这表明:
results | courseID | courseName | courseAuthor1 | courseAuthor2 | courseAuthor3 | courseAuthor4 | authorID | authorFirst | authorLast
1 | 1 | course1 | 2 | 3 | NULL | NULL | 2 | firstB | lastB
2 | 1 | course1 | 2 | 3 | NULL | NULL | 3 | firstC | lastC
3 | 2 | course 2 | 1 | 4 | 3 | NULL | 1 | firstA | lastA
4 | 2 | course 2 | 1 | 4 | 3 | NULL | 4 | firstD | lastD
5 | 2 | course 2 | 1 | 4 | 3 | NULL | 3 | firstC | lastC
6 | 3 | course 33 | 4 | 1 | 2 | NULL | 4 | firstD | lastD
7 | 3 | course 33 | 4 | 1 | 2 | NULL | 1 | firstA | lastA
8 | 3 | course 33 | 4 | 1 | 2 | NULL | 2 | firstB | lastB
9 | 4 | course 4 | 3 | NULL | NULL | NULL | 3 | firstC | lastC
非常感谢!