我有2张桌子:

表1:时间表

monday  tuesday  wednesday
------  -------  ---------
   18     15         2
   10      8         6


表2:对象

subject_id  subject_name
----------  ------------
   18           maths
   10          history
    8          english


结果应如下所示:

    monday   tuesday   wednesday
    ------- --------  ---------
    maths     phy       phy
   history  history     phy
   english    chem      phy


我该怎么做呢 ?

最佳答案

SELECT ms.subject_name as moday,
       ts.subject_name as tuesday,
       ws.subject_name as wednesday
FROM timetable t
INNER JOIN subjects ms ON t.monday = ms.subject_id
INNER JOIN subjects ts ON t.tuesday = ts.subject_id
INNER JOIN subjects ws ON t.wednesday = ws.subject_id

10-07 21:10