我对以下查询感到很头疼。我认为的问题是来自 AND 部分的重复返回结果。Mysql 为以下查询返回 14 个结果,但实际上只是 2 个不同的结果。
select date_format(published,'%Y-%m'),severity,count(severity)
FROM nvdcve
LEFT JOIN nvdproducts USING(cve_id)
where (published >= '2013-06-01')
AND (
(nvdproducts.company='linux' and nvdproducts.product='linux_kernel
AND nvdproducts.version IN (SELECT Kernel from Versions.VIEW_kernel))
OR (nvdproducts.company='apache' and nvdproducts.product='http_server'
AND nvdproducts.version IN (SELECT Httpd from Versions.VIEW_httpd))
OR (nvdproducts.company='sendmail' and nvdproducts.product='sendmail' AND nvdproducts.version IN (SELECT Sendmail from Versions.VIEW_sendmail))
OR (nvdproducts.company='mysql' and nvdproducts.product='mysql' AND nvdproducts.version IN (SELECT Mysqld from Versions.VIEW_mysqld))
OR (nvdproducts.company='proftpd' and nvdproducts.product='proftpd' AND nvdproducts.version IN (SELECT Proftpd from Versions.VIEW_proftpd))
OR (nvdproducts.company='perl' and nvdproducts.product='perl' AND nvdproducts.version IN (SELECT Perl from Versions.VIEW_perl))
OR (nvdproducts.company='openssl' and nvdproducts.product='openssl' AND nvdproducts.version IN (SELECT Sslinuse from Versions.VIEW_sslinuse))
)
group by date_format(published,'%Y-%m'),severity;
这个摘要查询给出了这个结果。
+--------------------------------+----------+-----------------+
| date_format(published,'%Y-%m') | severity | count(severity) |
+--------------------------------+----------+-----------------+
| 2013-06 | MEDIUM | 14 |
+--------------------------------+----------+-----------------+
我能得到的最接近的是获取行,但我失去了计数并添加了 cve_id,这不是我想要的。
select distinct cve_id,date_format(published,'%Y-%m'),severity
FROM nvdnew.nvdcve
LEFT JOIN nvdproducts USING(cve_id)
where (published > '2013-05-31')
and published < '2013-07-01'
AND (
(nvdproducts.company='linux' and nvdproducts.product='linux_kernel' AND nvdproducts.version IN (SELECT Kernel from Versions.VIEW_kernel))
OR (nvdproducts.company='apache' and nvdproducts.product='http_server' AND nvdproducts.version IN (SELECT Httpd from Versions.VIEW_httpd))
OR (nvdproducts.company='sendmail' and nvdproducts.product='sendmail' AND nvdproducts.version IN (SELECT Sendmail from Versions.VIEW_sendmail))
OR (nvdproducts.company='mysql' and nvdproducts.product='mysql' AND nvdproducts.version IN (SELECT Mysqld from Versions.VIEW_mysqld))
OR (nvdproducts.company='proftpd' and nvdproducts.product='proftpd' AND nvdproducts.version IN (SELECT Proftpd from Versions.VIEW_proftpd))
OR (nvdproducts.company='perl' and nvdproducts.product='perl' AND nvdproducts.version IN (SELECT Perl from Versions.VIEW_perl))
OR (nvdproducts.company='openssl' and nvdproducts.product='openssl' AND nvdproducts.version IN (SELECT Sslinuse from Versions.VIEW_sslinuse))
)
order by published;
这是结果。
+---------------+--------------------------------+----------+
| cve_id | date_format(published,'%Y-%m') | severity |
+---------------+--------------------------------+----------+
| CVE-2013-2128 | 2013-06 | MEDIUM |
| CVE-2013-1862 | 2013-06 | MEDIUM |
+---------------+--------------------------------+----------+
2 rows in set (0.00 sec)