简体   繁体   English

Mysql:从多个连接表中选择特定数据

[英]Mysql: Select specific data from multiple joined tables

I'm having trouble wrapping my brain around this select statement.我在思考这个 select 语句时遇到了麻烦。 The data is coming from 3 tables (I've cut out all unnecessary data for easier readability):数据来自 3 个表(为了便于阅读,我删除了所有不必要的数据):

mysql> describe vulnerability;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| vuln_id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| severity      | int(10) unsigned | NO   |     | NULL    |                |
| host_id       | int(10) unsigned | NO   | MUL | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+

mysql> describe cve;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| cve_id  | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| cve     | varchar(15)      | NO   |     | NULL    |                |
| vuln_id | int(10) unsigned | NO   | MUL | NULL    |                |
| year    | int(4) unsigned  | YES  |     | NULL    |                |
+---------+------------------+------+-----+---------+----------------+

mysql> describe host;
+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| host_id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| ip_addr      | int(10) unsigned | NO   |     | NULL    |                |
+--------------+------------------+------+-----+---------+----------------+

I want to output the number of hosts that have vulnerabilities that are less than the year 2009 with severity = 3. The year is contained in CVE, which is tied to Vulnerability with the vuln_id FK.我想输出具有小于 2009 年且严重性 = 3 的漏洞的主机数量。该年份包含在 CVE 中,它与具有 vuln_id FK 的漏洞相关联。 The vulnerability has the severity and is tied to Host with the host_id FK.该漏洞具有严重性,并与具有 host_id FK 的 Host 相关联。 Here is what I have so far:这是我到目前为止所拥有的:

mysql> select count(distinct ip_addr) from host H 
  inner join vulnerability V on H.host_id = V.host_id 
  inner join CVE C on C.vuln_id = V.vuln_id 
  where V.severity = 3 and C.year < 2009;
+-------------------------+
| count(distinct ip_addr) |
+-------------------------+
|                    5071 |
+-------------------------+

This tells me the total number of hosts with vulnerabilities older than 2009, which is a good start.这告诉我存在早于 2009 年的漏洞的主机总数,这是一个好的开始。 However, I want to take it a step further and only include those hosts that have 50 or more vulnerabilities.但是,我想更进一步,只包括那些有 50 个或更多漏洞的主机。 I'm not sure how to do this.我不知道该怎么做。 Each host entry in the Host table has multiple corresponding Vulnerability entries. Host 表中的每个主机条目都有多个对应的漏洞条目。 I assume I need to add something in my where clause, but I'm stuck.我想我需要在我的 where 子句中添加一些东西,但我被卡住了。

Thanks in advance.提前致谢。 Please let me know if more information is needed.如果需要更多信息,请告诉我。

Try using GROUP BY and HAVING :尝试使用GROUP BYHAVING

SELECT ip_addr
FROM host AS H
INNER JOIN vulnerability AS V
    ON H.host_id = V.host_id
INNER JOIN CVE AS C
    ON C.vuln_id = V.vuln_id
WHERE V.severity = 3 AND C.year < 2009
GROUP BY ip_addr
HAVING COUNT(DISTINCT vuln_id) >= 50

To just get the count wrap the above query inside another query:要获得计数,请将上述查询包装在另一个查询中:

SELECT COUNT(*) FROM
(
     SELECT ip_addr
     FROM host AS H
     -- etc... same query as above
) T1

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

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