-1

我有 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

非常感谢!

4

1 回答 1

0

我花了一段时间,但我已经回答了我的问题。这里是:

SELECT c1.courseID, c1.courseName, 
CONCAT(a1.authorFirst,' ',a1.authorLast) AS aOne, 
CONCAT(a2.authorFirst,' ',a2.authorLast) AS aTwo, 
CONCAT(a3.authorFirst,' ',a3.authorLast) AS aThree, 
CONCAT(a4.authorFirst,' ',a4.authorLast) AS aFour 
FROM courses c1 
LEFT JOIN authors a1 ON c1.courseAuthor1 = a1.authorID 
LEFT JOIN authors a2 ON c1.courseAuthor2 = a2.authorID 
LEFT JOIN authors a3 ON c1.courseAuthor3 = a3.authorID 
LEFT JOIN authors a4 ON c1.courseAuthor4 = a4.authorID
于 2013-04-15T14:41:56.397 回答