MySQL:将第一个查询的结果替换为第二个查询的结果

时间:2014-09-16 06:57:57

标签: php mysql

我有两张桌子:

表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

我该怎么做?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

以下是您要查找的查询:

SELECT SM.subject_name AS [monday]
    ,ST.subject_name AS [tuesday]
    ,SW.subject_name AS [wednesday]
FROM timetable T
INNER JOIN subjects SM ON SM.subject_id = T.monday
INNER JOIN subjects ST ON ST.subject_id = T.tuesday
INNER JOIN subjects SW ON SW.subject_id = T.wednesday

希望这会对你有所帮助。