SQL - Select between 3 tables
I have 3 tables:
hosts (host_id, name, address, template_id);
hostgroup (hostgroup_id, name);
hosts_hostgroup (a, b);
The first table keeps all the hosts, th开发者_运维技巧e second one all the groups and the last one says which group belong each host.
Note that hosts_hostgroup
has two different column names a
and b
.
Which is the Query to give me the hosts that belong to some group?
SELECT h.*
FROM hosts h
INNER JOIN hosts_hostgroup hg ON hg.host_id = h.host_id
INNER JOIN hostgroup g ON g.hostgroup_id = hg.hostgroup_id
WHERE g.name = 'mygroupname'
SELECT hosts.* FROM hosts
INNER JOIN hosts_hostgroup ON hosts.host_id=hosts_hostgroup.a
WHERE hosts_hostgroup.b=(some id);
精彩评论