SQL从table2

时间:2018-04-15 17:28:08

标签: mysql sql select join

你好我在MySQL DB中有2个表

status
id,    name,    date,    status
1      server1  datum    0
2      server2  datum    999
3      server3  datum    999
4      server4  datum    0

cis
id,    ,url
0      img/offline.png
1      img/server1.png
2      img/server2.png
3      img/server3.png
4      img/server4.png
999    img/online.png

我想从状态中选择ID和状态,我想用cis替换server_id和状态与url。但我不知道这是可行的,还是我需要为在线/在线网址创建第三个表?

我想加入表格,以便select返回

id,              status  
img/server1.png, img/offline.png
img/server2.png, img/online.png
img/server3.png, img/online.png
img/server4.png, img/offline.png

1 个答案:

答案 0 :(得分:1)

根据您的评论,您尝试了以下查询:

select s.id,s.name,s.date,s.status,c1.url as serverurl,c2.url as statusurl 
from status s left join cis c1 on s.id=c1.id left join cis c2 on s.status=c2.id

这将返回正确的结果集,但如果我理解您要查找的内容,则需要此查询:

SELECT  c1.url AS id, c2.url AS status FROM `status` s
LEFT JOIN `cis` c1 ON s.id = c1.id
LEFT JOIN `cis` c2 ON s.status = c2.id

c1.url别名为idc2.url别名为status