替换表中的列值

时间:2013-09-17 18:11:50

标签: mysql sql

我有三张桌子,我想在第一张桌子上看到来自其他人的价值观。让我解释一下:

课程表

+------------------+----------------+----------------+------------+-----------+
| LessonCode       | SubjectCode    | ClassCode      | hour       | day       |
+------------------+----------------+----------------+------------+-----------+
| 1                | 10             | 1              | 15         | m         |
| 2                | 11             | 2              | 20         | t         |
| 3                | 12             | 3              | 30         | w         |
+------------------+----------------+----------------+------------+-----------+

表类

+------------------+----------------+
| ClassCode        | ClassName      |
+------------------+----------------+
| 1                | Class1         |
| 2                | class2         |
| 3                | class3         |
+------------------+----------------+

表主题

+------------------+----------------+
| subjectCode      | SubjectName    |
+------------------+----------------+
| 10               | Maths          |
| 11               | English        |
| 12               | Greek          |
+------------------+----------------+

我想在lesson表格中插入SubjectNameClassName列代替SubjectCodeClassCode,并附上以下表格:< / p>

+------------------+----------------+----------------+------------+-----------+
| LessonCode       | SubjectName    | ClassName      | hour       | day       |
+------------------+----------------+----------------+------------+-----------+
| 1                | Maths          | Class1         | 15         | m         |
| 2                | English        | Class2         | 20         | t         |
| 3                | Greek          | Class3         | 30         | w         |
+------------------+----------------+----------------+------------+-----------+

我该怎么办?

1 个答案:

答案 0 :(得分:3)

这看起来像是JOIN的工作! INNER JOIN救援!

SELECT LessonCode, SubjectName, ClassName, hour, day
FROM lesson
JOIN class on lesson.ClassCode = class.ClassCode
JOIN subject on lesson.SubjectCode = subject.SubjectCode