mysql query show multiple tables from one ID column

无人久伴 提交于 2019-11-29 18:18:38

You have to join twice. Give the table different aliases so you can distinguish them.

SELECT h1.name as host_name, h2.name AS template_name
FROM hosts_template AS t
JOIN hosts AS h1 ON t.hostid = h1.hostid
JOIN hosts AS h2 ON t.hosttemplateid = h2.hostid

This is a basic question. You should learn more about SQL syntax, such as chained joins, accessing same column name from different tables.

Example code:

select h1.name, h2.name
from hosts_templates ht
    inner join hosts h1 on ht.hostid = h1.hostid
    inner join hosts h2 on ht.templateid = h2.hostid;

as you r selecting data from one table ie host_templates ony

SELECT id,hosts_templates.hostid,hosts_templates.templateid FROM hosts,host_templates WHERE hosts.id = hosts_templates.hostsid OR hosts.id=hosts_templates.templateid 

use it ,it works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!