繁体   English   中英

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

[英]MySQL: Replace result of first query with the result of 2nd query

我有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

这是您要查找的查询:

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

希望这会帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM