加入后mysql的结果不同

时间:2013-07-05 15:44:07

标签: mysql

我对以下查询有很多麻烦。我认为问题是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)

1 个答案:

答案 0 :(得分:1)

将所有nvdproducts项检查移至子查询中,以便您可以使用SELECT DISTINCT来防止重复。

SELECT DATE_FORMAT(published,'%Y-%m'), severity, COUNT(severity)
FROM nvdcve
LEFT JOIN (SELECT distinct cve_id
           FROM nvdproducts
           WHERE (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))
           ) nvdproducts
USING (cve_id)
WHERE published >= '2013-06-01'
GROUP BY DATE_FORMAT(published,'%Y-%m'), severity;