两桌
1.ClassSubjectTable和2.SubjectTable

类主题表

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


主题表

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


我想得到

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


而且,它们之间没有关系。可能吗?怎么样?

最佳答案

简单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

关于mysql - 一个表数据作为另一表列中的列标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34991345/

10-12 03:08