使用NOT-LIKE子句对三个表进行SQL查询

问题描述 投票:0回答:2

我需要提取不是指定组成员的所有服务器。我有3个表:host(包含主机),hostgroup_relation(包含主机ID和hostgroup id),hostgroup(包含hostgroups)

我可以获得关系,但我需要每个不属于该组成员的主机,其身份为180

主办:

SELECT host_id,host_name FROM host LIMIT 10;
+---------+-------------------+
| host_id | host_name         |
+---------+-------------------+
|    1482 | AADSYNC1          |
|     442 | Acces-Point-Wifi  |
|    1916 | ADAUDIT1          |
|    1562 | ADMORA1           |
|    2247 | ADMRDS2           |
|    2226 | ADSECU1           |
|    1203 | ADSELFSERVICE1    |
|    1172 | ALFRESCO1         |
|    1841 | ALFRESCO2         |
|     172 | Antispam-Ironport |
+---------+-------------------+

Hostgroups:

SELECT hg_id, hg_name FROM hostgroup LIMIT 10
+-------+----------------------+
| hg_id | hg_name              |
+-------+----------------------+
|    82 | Antivirus-Trend      |
|    65 | Autocoms             |
|    72 | Baies-de-stockage    |
|    78 | Consoles             |
|   192 | Databases-All        |
|   193 | Databases-Main       |
|    68 | Databases-MySql      |
|    67 | Databases-Oracle     |
|   181 | Databases-PostgreSql |
|    69 | Databases-SQLServer  |
+-------+----------------------+

主机/主机组关系:

SELECT * FROM hostgroup_relation LIMIT 10;
+--------+-----------------+--------------+
| hgr_id | hostgroup_hg_id | host_host_id |
+--------+-----------------+--------------+
|   5698 |              70 |         1167 |
|   6772 |              53 |         1167 |
|   6820 |             144 |         1369 |
|   6821 |              62 |         1369 |
|   6822 |              53 |         1369 |
|   6823 |              70 |         1369 |
|   6825 |              62 |         1370 |
|   6826 |              53 |         1370 |
|   6827 |              70 |         1370 |
|   6829 |              62 |         1371 |
+--------+-----------------+--------------+

这是我到目前为止的地方:

SELECT host.host_name, hostgroup.hg_name
FROM host, hostgroup_relation, hostgroup
WHERE hostgroup_relation.hostgroup_hg_id = hostgroup.hg_id
    AND hostgroup_relation.host_host_id = host.host_id
LIMIT 10;
+-----------+-----------------------------+
| host_name | hg_name                     |
+-----------+-----------------------------+
| AADSYNC1  | Default-bi                  |
| AADSYNC1  | Serveurs-Virtuels           |
| AADSYNC1  | Serveurs-Windows            |
| AADSYNC1  | Reboot_serveurs-12h00:14h00 |
| ADAUDIT1  | Default-bi                  |
| ADAUDIT1  | Serveurs-Virtuels           |
| ADAUDIT1  | Serveurs-Windows            |
| ADAUDIT1  | Reboot_serveurs-12h00:14h00 |
| ADMORA1   | Default-bi                  |
| ADMORA1   | Reboot_serveurs-00h00:4h00  |
+-----------+-----------------------------+

我需要一个不在指定组中的所有服务器的列表。

mysql relation
2个回答
1
投票

你需要做一个简单的连接。

这可能有效:

SELECT h.host_id, h.host_name
FROM host h
LEFT OUTER JOIN hostgroup_relation hgr ON (hgr.host_host_id = h.host_id AND hgr.hostgroup_hg_id = 180)
WHERE hgr.hgr_id IS NULL

-1
投票

试试这个:

    select h.host_id, hg.host_name, hg.hg_id
    from host h
    join hostgroup_relation hg
    where h.host_id = hg.host_host_id
    and hg_id not in 180;
© www.soinside.com 2019 - 2024. All rights reserved.