一个表数据作为另一个表列中的列标题

时间:2016-01-25 11:20:41

标签: mysql database

两张表 1.ClassSubjectTable和2.SubjectTable

ClassSubjectTable

------------------------------------
class_subject_id |subject1 |subject2
------------------------------------
  1 (General Id) |   1      |    2  -----> are subject_id's

SubjectTable

-------------------------
subject_id | subject_name
-------------------------
      1    |  Maths
      2    |  Science

我想要

------------------------------------
class_subject_id |subject1 |subject2
------------------------------------
       1         |Maths    | Science

他们之间没有任何关系。可能吗?怎么样?

1 个答案:

答案 0 :(得分:1)

简单JOIN

SELECT c.class_subject_id, s1.subject_name AS subject1, s2.subject_name AS subject2
FROM ClassSubjectTable c
LEFT JOIN SubjectTable s1
  ON c.subject1 = s1.subject_id
LEFT JOIN SubjectTable s2
  ON c.subject2 = s2.subject_id;

SqlFiddleDemo